1 回答
TA贡献1963条经验 获得超6个赞
你几乎已经明白了,但matlab.Execute("cd d:\textcreator.m")你应该,而不是matlab.Execute("cd d:\"),然后matlab.Execute("run textcreator.m")。所以你的代码应该是:
MLApp.MLApp matlab = new MLApp.MLApp();
matlab.Execute("cd d:\");
matlab.Execute("run textcreator.m");
我还挖出了我很久以前写的一个简单的 MLApp 包装器。认为它会对你有用。
class MLWrapper
{
private readonly MLApp.MLApp _mlapp;
public MLWrapper(bool visible = false)
{
_mlapp = new MLApp.MLApp();
if (visible)
ShowConsole();
else
HideConsole();
}
~MLWrapper()
{
Run("close all");
_mlapp.Quit();
}
public void ShowConsole()
{
_mlapp.Visible = 1;
}
public void HideConsole()
{
_mlapp.Visible = 0;
}
/// <summary>
/// Run a MATLAB command.
/// </summary>
/// <returns>Text output displayed in MATLAB console.</returns>
public string Run(string cmd)
{
return _mlapp.Execute(cmd);
}
/// <summary>
/// Run a MATLAB script.
/// </summary>
/// <returns>Text output displayed in MATLAB console.</returns>
public string RunScript(string scriptName)
{
return Run($"run '{scriptName}'");
}
/// <summary>
/// Change MATLAB's current working folder to the specified directory.
/// </summary>
public void CD(string directory)
{
Run($"cd '{directory}'");
}
public object GetVariable(string varName)
{
_mlapp.GetWorkspaceData(varName, "base", out var data);
return data;
}
public void SetVariable(string varName, object value)
{
_mlapp.PutWorkspaceData(varName, "base", value);
}
}
- 1 回答
- 0 关注
- 119 浏览
添加回答
举报