“无效的被叫者"在C#中使用MLapp时 [英] "Invalid Callee" while using MLapp in C#

查看:222
本文介绍了“无效的被叫者"在C#中使用MLapp时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用 MLApp.GetWorkspaceData 功能.我注意到当执行以下操作时,此功能可以正常工作:

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);

这非常麻烦,因为我需要从Matlab到Visual Studio获取大量数据,并且实际上无法创建52K未初始化变量并将其保留.有某种方法可以取消初始化"变量吗?我在这里缺少一些概念吗?

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?

推荐答案

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

这是一个经过测试的示例从C#调用MATLAB :

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]);
                }
            }
        }
    }
}
}

输出:

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

这篇关于“无效的被叫者"在C#中使用MLapp时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆