使用 MLApp 和 COM、C# 在 matlab 实例中修改数据

Modify data in matlab instance using MLApp and COM, C#

C# 应用程序试图在 Matlab 中重塑数据。在 Matlab 中,我需要数据在 Matlab 实例 中显示 3 维。 (代码假定 matlab 的一个实例是 运行。)

public void PassAndResizeInMatlab()
{
    MLApp.MLApp matlab = (MLApp.MLApp)Marshal.GetActiveObject("Matlab.Desktop.Application");
    matlab.Execute("enableservice('AutomationServer',true);");
    var dat = new double[]{1,2,3,4};
    var name = "myexample";
    //matlab does not support passing double[,,] with this function.
    matlab.PutWorkspaceData(name, "base", dat);

    object varargout;
    //this fails
    matlab.Feval("reshape", 1, out varargout, name, new double[]{2,2});
    //works but does not put the value in the matlab instance.
    matlab.Feval("ones", 1, out varargout, 3,4,5); //works
    //works but does not put the value in the matlab instance.
    var output = matlab.Execute("reshape (" + name + ",2,2)");
}

是否可以使用 COM 修改 matlab 中的现有数据?

需要在execute方法中设置值:

matlab.Execute(name + " = reshape (" + name + ",2,2)");