亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 C# 中運行 Matlab 腳本

在 C# 中運行 Matlab 腳本

C#
一只甜甜圈 2022-12-24 13:56:38
我有創建一些結果文件 output.txt 的 matlab 腳本 textcreator.m。并且有一些matlab.aplication()參考將 matlab 函數“翻譯”為 c#,并且一些代碼很難轉換為 c#,我決定只運行我制作的腳本。using System; using System.Collections.Generic; using System.Text; MLApp.MLApp matlab = new MLApp.MLApp(); matlab.Execute(@"cd d:\textcreator.m"); 當我在裝有 Matlab 的電腦上單擊按鈕時如何運行 matlab 腳本 textcreator.m?
查看完整描述

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

    }

}


查看完整回答
反對 回復 2022-12-24
  • 1 回答
  • 0 關注
  • 143 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號