“Invalid Callee” while using MLapp in C#

怎甘沉沦 提交于 2019-12-25 02:25:14

问题


I'm having a strange issue while using the MLApp.GetWorkspaceData function. I noticed that this functions works properly when I do the following:

matlab = new MLApp.MLAppClass();

object myObject;

matlab.GetWorkspaceData("myVariable", "base", out myObject);

But if I then try to use the same object as an output I get an "Invalid Callee" exception. In addition this also gives the same error:

matlab = new MLApp.MLAppClass();

object myObject = new object();

matlab.GetWorkspaceData("myVariable", "base", out myObject);

This is very troublesome because I need to get a large amount of data from Matlab to Visual Studio, and I cannot practically create 52K uninitialized variables and keep them around. Is there some way to "uninitialize" a variable? Is there some concept I'm missing here?


回答1:


As @wonko79 explained in the comments, if you want to reuse the out variable, you should set it to null first.

Here is a tested example calling MATLAB from C#:

using System;

namespace CSharp_matlab_com
{
class Program
{
    static void Main(string[] args)
    {
        MLApp.MLAppClass matlab = new MLApp.MLAppClass();

        // create variables: a_0, a_1, ..., a_4
        for (int k = 0; k < 5; k++) {
            matlab.Execute(string.Format("a_{0} = rand(2);", k));
        }

        // retrieve variables from MATLAB and print their contents
        object a;
        for (int k = 0; k < 5; k++) {
            // current variable name
            string varname = string.Format("a_{0}", k);

            // get data array
            a = null;    // without this line, an exception is thrown!
            matlab.GetWorkspaceData(varname, "base", out a);

            // print contents
            var arr = (double[,]) a;
            Console.WriteLine("\nndims(a) = {0}, numel(a) = {1}", arr.Rank, arr.Length);
            for (int i = 0; i < arr.GetLength(0); i++) {
                for (int j = 0; j < arr.GetLength(1); j++) {
                    Console.WriteLine("{0}[{1},{2}] = {3}", varname, i, j, arr[i,j]);
                }
            }
        }
    }
}
}

The output:

ndims(a) = 2, numel(a) = 4
a_0[0,0] = 0.251806122472313
a_0[0,1] = 0.617090884393223
a_0[1,0] = 0.290440664276979
a_0[1,1] = 0.265280909810029

...

ndims(a) = 2, numel(a) = 4
a_4[0,0] = 0.425259320214135
a_4[0,1] = 0.16148474431175
a_4[1,0] = 0.312718886820616
a_4[1,1] = 0.178766186752368



回答2:


You can create a wrapper for GetWorkspaceData method, like in the next example:

public object GetData(string name)
{
    object data;
    mlApp.GetWorkspaceData(name, "base", out data);

    return data;
}

Or, even more useful, a generic wrapper:

public T GetData<T>(string name)
{
    object data;
    mlApp.GetWorkspaceData(name, "base", out data);

    if (data == null)
        return default(T);

    if (data is T)
        return (T)data;
    else
        throw new InvalidCastException($"The variable '{name}', of type '{data.GetType().Name}' cannot be casted to type '{typeof(T).Name}'.");
}



回答3:


The solution is to set the output object to null.

I found it here.



来源:https://stackoverflow.com/questions/21118710/invalid-callee-while-using-mlapp-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!