0

I have a function in Matlab and I want to call it from the command prompt which is started from C# program, and get the result code

Matlab function

function func(v1,v2)
v1
v2
end

I call it with

matlab -wait -r "func('hello','goodbye');"

The function executes normally and finishes but the command prompt is still waiting and when the matlab program close the command prompt becoming free but I want to get the exit code

Actually I start this command prompt from a C# program and I want to understand if the function is successfully executed or not. when I close the cmd manually I get the error in C# program however the matlab function is done successfully. I want to get the related exit code in C# program

My C# code is like:

strMatlabCommand = "/K matlab -wait -sd \"D:\\directory\" -r \"func('" + variable1 + "','"+ variable2 + "' )\" ;";

                mcdProcess = new System.Diagnostics.Process();
                
                mcdProcess.StartInfo.FileName = "cmd.exe";
                mcdProcess.StartInfo.Arguments = strMatlabCommand;

                mcdProcess.Start();



                mcdProcess.WaitForExit();

                this.Text = mcdProcess.ExitCode.ToString();

                if (mcdProcess.ExitCode == 0)
                {
                    MessageBox.Show("Process Finished Successfully", "Result", MessageBoxButtons.OK);
                }
                else
                {
                    MessageBox.Show("Process Finished Unsuccessfully!!!!", "Result", MessageBoxButtons.OK);
                }

any help would be appreciated

ali abdari
  • 27
  • 7
  • Please do provide the code in c# that you are using along with the error message – Jawad Jul 03 '20 at 16:12
  • dear @Jawad I added the C# code in the question body. thank you very much – ali abdari Jul 03 '20 at 17:56
  • Check task manager and see if matlab is running. What I think is happening is matlab is closing before your application has read all the return data. From following posting it looks like it should be /r (not -r). See : https://www.mathworks.com/matlabcentral/answers/320908-how-to-start-matlab-from-command-prompt-and-wait-for-the-application-to-return It also looks like you need a script file not a function. – jdweng Jul 03 '20 at 18:02
  • 2
    The arguments to MATLAB are correct. But you need to call `exit` in MATLAB otherwise it will not terminate after finishing running the command. `matlab -wait -r "func('hello','goodbye'); exit"` – Cris Luengo Jul 03 '20 at 19:32
  • dear @CrisLuengo when I use exit at the end of my command the Matlab is closed after the execution and the cmd go out of waiting mode but still, there is no reaction in the C# program – ali abdari Jul 05 '20 at 08:06
  • 1
    You also use the `/k` switch to `cmd.exe`. Why? Read the docs!!! It is meant to pause after the command terminates. You want to use `/c` instead. Or not use `cmd` at all, just run `matlab`. – Cris Luengo Jul 05 '20 at 13:51
  • dear @CrisLuengo I changed the switch to ```/C``` and fortunately, it worked for successful cases. But one problem still exists. In the cases that the Matlab execution crashed it seems that the ```exit``` command does not execute and therefore the Matlab and cmd won't close to send the proper message to C# program. I think this issue happens when the first command does not execute successfully. Thank you very much – ali abdari Jul 05 '20 at 14:25
  • If you want to accept the function throwing an error, you need to put a try/catch block around the function call. But you will not get a status code back from MATLAB. The only way to do that is [using the new `-batch` argument](https://stackoverflow.com/a/58358304/7328782) (since R2019b). – Cris Luengo Jul 05 '20 at 14:38

0 Answers0