I'm working with a command line Matlab (i.e. remote ubuntu linux workstation) and am trying to execute a script.
When I ran:
matlab -nodesktop -nosplash -r "my_script.m"
matlab would open and give me an error message stating
Undefined variable "my_script" or class "my_script.m"
I found a thread on stackoverflow with a user having a similar problem:
Matlab: Running an m-file from command-line
but when I attempted to implement the suggested syntax:
matlab -nodesktop -nosplash -r "run('my_script.m');"
I now get a syntax error,
Unexpected MATLAB expression.
Error in run (line 96)
evalin('caller', [script ';'])
I seldom use Matlab and even more rarely do so w/o a gui, so I've been trying without success to fix the syntax using information from online messageboards.
You just need to remove .m extension:
matlab -nodesktop -nosplash -r "my_script"
The reason is that my_script.m is not a valid Matlab statement. In order to run a script / function you need to execute it using its name, i.e. my_script. You can see that if you try running my_script.m and my_script statements right from Matlab command window.
The second error you mention (when using run command) seems to be the actual error in your script. It seems like you forgot to copy-paste the very top line which should show the line number where the error occurs. What you see below, i.e. Error in run (line 96) and evalin('caller', [script ';']) is just a second entry in the stack trace. It does confirm the the error occurs while evaluating your script using evalin.
This question already has answers here:
Writing log statements to standard output with Matlab
(8 answers)
Closed 6 years ago.
I'm trying to integrate my MATLAB unit tests into Jenkins in a Windows environment. My problem is that I am not able to get the MATLAB output in my Jenkins console, even for a simple disp('Hello World!').
I create a Jenkins free job to execute the following batch command:
matlab -nodisplay -r "disp('Hello World!');exit".
Here is my result:
C:\Jenkins\jobs\runAllTests\workspace>matlab -nodisplay -r "disp('Hello World!');exit"
C:\Jenkins\jobs\runAllTests\workspace>exit 0
Finished: SUCCESS
Of course, I want to use Jenkins to execute a script to run several unit tests. But the problem is the same, I am not able to catch the MATLAB outputs.
Thanks in advance for any help.
I finally manage to get the matlab output in the console.
I follow a tip given in #AndyCampbell blog by #Guy Starbuck:
start /wait matlab -nodesktop -nosplash -minimize -wait -r "disp('Hello World!');exit" -logfile unittestlog.txt
set output=%errorlevel%
MORE unittestlog.txt
EXIT %output%
thanks all for your help.
You need to add -wait to the MATLAB command.
On Windows, Jenkins wraps the command in a batch file that returns immediately, and therefore doesn't capture the output (and by the way, it will also always exit with a success status even if MATLAB itself didn't).
By adding -wait, it will delay the exit until MATLAB has finished, and it will also return with the appropriate exit status.
PS Also see this excellent series of posts by #AndyCampbell on integrating MATLAB with Jenkins.
Edit:
The above works for me. But here's a couple of other things I would check, as they've been gotchas for me when I was getting it set up - perhaps they will help you too:
Make sure the build step is an "Execute Windows batch command" step rather than an "Execute shell" step, as it's a pain to get the unix utilities installed and running on Windows
Make sure that you have the right type of quote marks in the MATLAB build command. They need to be straight quotes, not curly quotes - both the single and double ones
If you copied and pasted the MATLAB build command into Jenkins, make sure you didn't accidentally paste in any extra invisible characters - try typing the command directly into Jenkins
Make sure there are no licensing issues - for example, Jenkins may be running as user1 and will call MATLAB as user1, but MATLAB is licensed to user2. If you call MATLAB with -nodesktop in this case it will just silently fail (and may even leave a zombie MATLAB process hanging around, with an invisible license error dialog, that you can only quit from with Task Manager)
To assist in troubleshooting, you can add -logfile \path\to\logfile.txt to your MATLAB command, which can diagnose some issues. You can also use a startup.m file and/or a finish.m file - these should run at startup (before your build command) and just before exit (after your build command). Finally you could try using a build command that does something simple to the filesystem, rather than a disp (this would diagnose whether it's an issue with MATLAB running at all, or an issue with Jenkins collecting its output).
Suppose that;
I have an m-file at location:
C:\M1\M2\M3\mfile.m
And exe file of the matlab is at this location:
C:\E1\E2\E3\matlab.exe
I want to run this m-file with Matlab, from command-line, for example inside a .bat file. How can I do this, is there a way to do it?
A command like this runs the m-file successfully:
"C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "run('C:\<a long path here>\mfile.m'); exit;"
I think that one important point that was not mentioned in the previous answers is that, if not explicitly indicated, the matlab interpreter will remain open.
Therefore, to the answer of #hkBattousai I will add the exit command:
"C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "run('C:\<a long path here>\mfile.m');exit;"
Here is what I would use instead, to gracefully handle errors from the script:
"C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "try, run('C:\<a long path here>\mfile.m'), catch, exit, end, exit"
If you want more verbosity:
"C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "try, run('C:\<a long path here>\mfile.m'), catch me, fprintf('%s / %s\n',me.identifier,me.message), end, exit"
I found the original reference here. Since original link is now gone, here is the link to an alternate newreader still alive today:
exit matlab when running batch m file
On Linux you can do the same and you can actually send back to the shell a custom error code, like the following:
#!/bin/bash
matlab -nodisplay -nojvm -nosplash -nodesktop -r \
"try, run('/foo/bar/my_script.m'), catch, exit(1), end, exit(0);"
echo "matlab exit code: $?"
it prints matlab exit code: 1 if the script throws an exception, matlab exit code: 0 otherwise.
Here are the steps:
Start the command line.
Enter the folder containing the .m file with cd C:\M1\M2\M3
Run the following: C:\E1\E2\E3\matlab.exe -r mfile
Windows systems will use your current folder as the location for MATLAB to search for .m files, and the -r option tries to start the given .m file as soon as startup occurs.
Since R2019b, there is a new command line option, -batch. It replaces -r, which is no longer recommended. It also unifies the syntax across platforms. See for example the documentation for Windows, for the other platforms the description is identical.
matlab -batch "statement to run"
This starts MATLAB without the desktop or splash screen, logs all output to stdout and stderr, exits automatically when the statement completes, and provides an exit code reporting success or error.
It is thus no longer necessary to use try/catch around the code to run, and it is no longer necessary to add an exit statement.
cat 1.m | matlab -nodesktop -nosplash
And I use Ubuntu
Thanks to malat. Your comment helped me.
But I want to add my try-catch block, as I found the MExeption method getReport() that returns the whole error message and prints it to the matlab console.
Additionally I printed the filename as this compilation is part of a batch script that calls matlab.
try
some_code
...
catch message
display(['ERROR in file: ' message.stack.file])
display(['ERROR: ' getReport(message)])
end;
For a false model name passed to legacy code generation method, the output would look like:
ERROR in file: C:\..\..\..
ERROR: Undefined function or variable 'modelname'.
Error in sub-m-file (line 63)
legacy_code( 'slblock_generate', specs, modelname);
Error in m-file (line 11)
sub-m-file
Error in run (line 63)
evalin('caller', [script ';']);
Finally, to display the output at the windows command prompt window, just log the matlab console to a file with -logfile logfile.txt (use additionally -wait) and call the batch command type logfile.txt
I run this command within a bash script, in particular to submit SGE jobs and batch process things:
/Path_to_matlab -nodisplay -nosplash -nodesktop < m_file.m
Since none of the answers has information about feeding input argument, it is important to
add it here. After some research, I found this link
Feeding the arguments is very similar to how we run a Matlab function.
matlab -r 'try myfunction(argument1,argument2); catch; end; quit'
If you are somehow getting an argument from bash/terminal, you simply need to insert that into the bash command as:
matlab -r 'try myfunction($MY_BASH_ARG,argument2); catch; end; quit'
(This is after a couple of trial and error)
I've a Matlab R2007b script which I call from a batch script like this:
matlab.exe -nosplash -nodesktop -r my_script
I've a try/catch block in the Matlab script and on error it runs exit(1), to indicate to the calling process (indirectly, Jenkins!) that the process has failed.
This works great except occasionally on exit via error Matlab wants to save any modified files and pops up a save dialogue (not great when you're running headless with Jenkins!). I can avoided this with a 'exit force' but then my return code is always 0 (indicating success to Jenkins).
Is it possible in Matlab to force exit AND set a return code greater than zero?
exit(1) force
I have also tried to figure out a solution to this issue. Since working with environment variables did not work out the way we wanted, I created a workaround with a dummy file that is created whenever the Matlab call fails and then checked in Jenkins (currently no Matlab access, maybe some quotes are wrong):
matlab -r "try, [returnVal]=ScriptName;catch, disp('exception occurred']); returnVal= -1; end, if (returnVal~= 0), fid = fopen('errorFile.txt','wt'); fclose(fid); end, exit force"
IF EXIST errorFile.txt (exit /b 1)
Generally, of course, it also makes sense to include additional arguments in the matlab call, for Windows, e.g.:
matlab -log -nosplash -nodesktop -noFigureWindows -minimize -wait
I see in the MATLAB help (matlab -h) that I can use the -r flag to specify an m-file to run. I notice when I do this, MATLAB seems to start the script, but immediately return. The script processes fine, but the main app has already returned.
Is there any way to get MATLAB to only return once the command is finished? If you're calling it from a separate program it seems like it's easier to wait on the process than to use a file or sockets to confirm completion.
To illustrate, here's a sample function waitHello.m:
function waitHello
disp('Waiting...');
pause(3); %pauses 3 seconds
disp('Hello World');
quit;
And I try to run this using:
matlab -nosplash -nodesktop -r waitHello
Quick answer:
matlab -wait -nosplash -nodesktop -r waitHello
In Matlab 7.1 (the version I have) there is an undocumented command line option -wait in matlab.bat. If it doesn't work for your version, you could probably add it in. Here's what I found. The command at the bottom that finally launches matlab is (line 153):
start "MATLAB" %START_WAIT% "%MATLAB_BIN_DIR%\%MATLAB_ARCH%\matlab" %MATLAB_ARGS%
The relevant syntax of the start command (see "help start" in cmd.exe) in this case is:
start ["window title"] [/wait] myprogram.exe args ...
A bit higher, among all of the documented command line options, I found (line 60):
) else if (%opt%) == (-wait) (
set START_WAIT=/wait
) else (
So specifying -wait should do what you want, as long as you're also exiting matlab from your script (otherwise it will wait for you to terminate it interactively).