“无效的Calle”而使用MLapp在C# [英] "Invalid Calle" while using MLapp in C#
问题描述
使用 MLApp.GetWorkspaceData 时遇到了问题功能。我注意到,如果我做以下这个功能适当地工作:
I'm having strange issue while using the MLApp.GetWorkspaceData function. I notice that this functions works appropriately if 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);
这是非常麻烦的,因为我需要从Matlab到Visual Studio获得大量的数据,我实际上不能创建52K未初始化的变量,并保持它们。有没有办法uninitialize一个变量?
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?
推荐答案
由于 out $ c> / 21118710 / invalid-calle-while-use-mlapp-in-c-sharp#comment31776934_21118710> @ wonko79 $ c>变量,你应该首先将它设置为 null
。
As @wonko79 explained in the comments, if you want to reuse the out
variable, you should set it to null
first.
这里是一个测试示例从C#调用MATLAB :
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]);
}
}
}
}
}
}
输出:
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
这篇关于“无效的Calle”而使用MLapp在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!