bat file for running MATLAB code from Notepad++ - matlab

I want to write a .bat file which, when I push the run button on Notepad++,
runs my matlab code through the command line
I wrote a simple batch file for running specific code (test.m)
How can I pass other file addresses through Notepad++'s run button to my batch file?
I would also appreciate other solutions.
My code:
"C:\Program Files\MATLAB\R2014b\bin\matlab.exe" -nodisplay -nosplash -nodesktop -r "; run('D:\test.m');"

This one I wouldn't use a CMD command, but rather a batch file that runs certain parts of your program. If I understand you correctly, then you want to run the MATLAB program when you click on the icon, as well as notepad++. What I would do is the following.
Right Click on the program, select properties, then change the target line to
"[Notepad++ Target Path Here]", "C:\Program Files\MATLAB\R2014b\bin\matlab.exe" -nodisplay -nosplash -nodesktop -r "; run('D:\test.m');"
This should run both programs
Hope this helps!

Related

Run MATLAB script on cmd without opening the GUI window or the icon [duplicate]

I am running a MATLAB script from the Windows command prompt:
"C:\Program Files\MATLAB\R2014B\bin\matlab" -nodisplay -nosplash -nodesktop -wait -r "test.m"
The test.m is simple:
function test
disp('Hello!');
The output is displayed in the Matlab Command Window. Is there any way how I can force output to the windows prompt?
Use the command line option -log when you call Matlab from the command line (or any other shell or batch (e.g. cmd or bat) script).
It isn't documented as of Matlab 2017b, but it works.
Side note: -nodisplay isn't supported in the Windows version of Matlab, but if you want to prevent it from displaying figures, use -noFigureWindows instead.
Since R2019b, there is a new command line option, -batch, which redirects the output to the command line and handles other stuff for batch processing. See the documentation for Windows.
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.
I don't know of a way to do it in Windows to get Matlab to actually run in the DOS window which is what you would need for the display to be written in it. (FYI: You can in LINUX - but I assume you need to run in Windows).
For running in Matlab you have 2 alternatives that I can think of:
-logfile FILE on launch which will record all output to a FILE you specify - however how and when the file is written to disk is controlled by Matlab and I haven't tested to see - if your code doesn't do much it might only be written on Matlab exit.
diary FILE in your Matlab command, i.e. -r "diary FILE.TXT; test.m; diary OFF" - this is similar to above - but uses the diary function.
However you can get what you want if you can run your code compiled (I know thats a big if as you may not have the compiler - or if you regularly want to update test.m this is not the most efficient...
When you run a compiled code from a DOS prompt all the terminal messages are written to the DOS prompt. One thing I'd advise if this is suitable is to delete the "splash.png" file from your installation directory to avoid the splash screen displaying when you run from the DOS as its (probably) not needed.
I have found a solution at:
https://www.mathworks.com/matlabcentral/answers/91607-how-can-i-redirect-the-command-window-output-to-stdout-and-stderr-when-running-matlab-7-8-r2009a-i#answer_100958
I will replicate it here for convenience.
First I need to modify the matlab script to output to a text file:
function test
fid=fopen('output.txt','w');
fprintf(fid,'Hello!');
fclose(fid);
Then I should run the Matlab using a bat file with one additional command to display the contents of the output.txt:
"C:\Program Files\MATLAB\R2014B\bin\matlab" -nodisplay -nosplash -nodesktop -wait -r "test.m"
type output.txt
The type command will display the contents of 'output.txt' in the command window. So answer from #matlabgui was almost there. Thank you.
It is not a very elegant solution, but it works.

How to run matlab from the command line and print to the command line?

I want to run a script on windows dos terminal where the script will display "Hello world" to the terminal I executed this from e.g.
matlab.exe -nosplash -nodesktop -nojvm -wait -r printToCommandLine.m
Where printToCommandLine.m contains:
system(sprintf('echo Hello world'));
but it only prints to the matlab command window that gets generated when executing the script
First, I am not shure, if the syntax has changed, but I have to call the script without the file extension '.m':
matlab.exe -nosplash -nodesktop -nojvm -wait -r printToCommandLine
Otherwise I will get an error within MATLAB.
Second, this is just a work around, but you can print your current command line output to a log file e.g. 'log.txt' using
matlab.exe -nosplash -nodesktop -nojvm -wait -logfile "log.txt" -r printToCommandLine
The log file will be updated at runtime. To test this, I created a small example script and had a look how 'log.txt' changes during execution:
disp('Script execution started. Waiting 10 seconds...')
pause(10)
disp('...waited 10 seconds.');
This is not exactly what you wanted but it gives you the chance to get actual information about the current command line output during your execution (in a text file).
We use this for automated (remote) testing to print our MATLAB command line output to the console after the tests pass with
type log.txt

Force Matlab output to command line

I am running a MATLAB script from the Windows command prompt:
"C:\Program Files\MATLAB\R2014B\bin\matlab" -nodisplay -nosplash -nodesktop -wait -r "test.m"
The test.m is simple:
function test
disp('Hello!');
The output is displayed in the Matlab Command Window. Is there any way how I can force output to the windows prompt?
Use the command line option -log when you call Matlab from the command line (or any other shell or batch (e.g. cmd or bat) script).
It isn't documented as of Matlab 2017b, but it works.
Side note: -nodisplay isn't supported in the Windows version of Matlab, but if you want to prevent it from displaying figures, use -noFigureWindows instead.
Since R2019b, there is a new command line option, -batch, which redirects the output to the command line and handles other stuff for batch processing. See the documentation for Windows.
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.
I don't know of a way to do it in Windows to get Matlab to actually run in the DOS window which is what you would need for the display to be written in it. (FYI: You can in LINUX - but I assume you need to run in Windows).
For running in Matlab you have 2 alternatives that I can think of:
-logfile FILE on launch which will record all output to a FILE you specify - however how and when the file is written to disk is controlled by Matlab and I haven't tested to see - if your code doesn't do much it might only be written on Matlab exit.
diary FILE in your Matlab command, i.e. -r "diary FILE.TXT; test.m; diary OFF" - this is similar to above - but uses the diary function.
However you can get what you want if you can run your code compiled (I know thats a big if as you may not have the compiler - or if you regularly want to update test.m this is not the most efficient...
When you run a compiled code from a DOS prompt all the terminal messages are written to the DOS prompt. One thing I'd advise if this is suitable is to delete the "splash.png" file from your installation directory to avoid the splash screen displaying when you run from the DOS as its (probably) not needed.
I have found a solution at:
https://www.mathworks.com/matlabcentral/answers/91607-how-can-i-redirect-the-command-window-output-to-stdout-and-stderr-when-running-matlab-7-8-r2009a-i#answer_100958
I will replicate it here for convenience.
First I need to modify the matlab script to output to a text file:
function test
fid=fopen('output.txt','w');
fprintf(fid,'Hello!');
fclose(fid);
Then I should run the Matlab using a bat file with one additional command to display the contents of the output.txt:
"C:\Program Files\MATLAB\R2014B\bin\matlab" -nodisplay -nosplash -nodesktop -wait -r "test.m"
type output.txt
The type command will display the contents of 'output.txt' in the command window. So answer from #matlabgui was almost there. Thank you.
It is not a very elegant solution, but it works.

How to open a matlab program using windows command prompt? (in a single window) [duplicate]

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)

Run a MATLAB script from Windows DOS prompt

I am trying to run a Matlab script from Windows command prompt but I can't execute it sometimes. The script runs fine when manually launched. Matlab version is 2011a and Windows is Server 2003 SP2. Details:
Script mytask.m is located inside say E:\Production\Project. This is SAVED on Matlab's path.
When I place mytask.m inside bin folder, it executes fine by the command:
`C:\Program Files\MATLAB\R2011a\bin>matlab -r mytask`
If you delete it and try to access it at its original location, the script doesn't run although Matlab editor window is launched:
`C:\Program Files\MATLAB\R2011a\bin>matlab -r "E:\Production\Project\mytask"
Any suggestions please? Thanks.
The syntax for matlab -r is
matlab -r "statement"
In other words, you need to provide some executable commands as the statement. For example:
matlab -r "run E:\Production\Project\mytask"
However, it seems that matlab does not load the customized paths in this way. If you have some customized paths, you probably have to define them in startup.m and place this startup.m in the directory where you invoke matlab.
I didn't check myself, but if you define E:\Production\Project\ as the path in startup.m, you probably can run matlab -r mytask without problem, as mytask will be recognized as a user function/script.
A simple example of startup.m
path(path, 'E:\Production\Project\');