Matlab script call from command line first calls startup? - matlab

I have a startup Matlab script startup.m under
C:\SVN\myscripts\StartupScript
The script sets up paths and does some subsequent script calling successfully.
However, i created another file callMat.m in the same folder that just displays some data. When i run it from Matlab GUI, it works fine.
Howeever, calling it from cmd.exe,like below:
C:\Program Files\MATLAB\R2013b\bin\matlab.exe" -nosplash -nodesktop -wait -r "callMat"
I found that it opens Matlab command line and then
it first executes startup.m and then callMat.m .
Is this expected behavior, and if yes Why?
Thanks
sedy

Yes, this is expected behaviour. MATLAB executes startup.m when it starts up, whether it's started in the usual way, or whether it's started from the command line with -r and a command.

startup.m on your Matlab path is executed on startup.
I can find no description on the Matlab website suggesting that command line starting is any different.
In conclusion, it seems very likely this is expected behaviour. The documentation page here gives some suggestions as to the different places to put different options. In particular, it suggests that you:
Use startup.m to modify the default search path, predefine variables
in your workspace, or define defaults for graphics objects.

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.

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 run octave script from shell at launch

I'm using Ubuntu system. To run MATLAB script at launch, I can type matlab -nodesktop -r "run ./my_program.m".
How can I achieve the same function on Octave, like octave --no-gui -some_command?
I've read this post and this post. They did not answer my question.
As mentioned in one of the comment, one solution is:
octave --persist my_program.m
Currently looks like it is enough to say for script (not plain function)
octave my_program.m
Also if you have octave installed in your path you could and in the beginning of script as the first line the desired interpreter (works for python, bash, whatever), for example for default location :
#!/usr/bin/octave
and you can run them without even adding the octave in front... just the script name....
For Windows users:
I see it's a bit harder, since there's no "octave.exe" file.
I do have:
"C:\Program Files\GNU Octave\Octave-6.4.0\mingw64\bin\octave-cli-6.4.0.exe"
which works after adding both parent directories
"C:\Program Files\GNU Octave\Octave-6.4.0\mingw64\bin"
and
"C:\Program Files\GNU Octave\Octave-6.4.0\mingw64"
to PATH.
All this happens automatically in the startup script which is the normal entry point of the SW:
"C:\Program Files\GNU Octave\Octave-6.4.0\octave.vbs"
Finaly, after adding all to path I run it like this:
"C:\Program Files\GNU Octave\Octave-6.4.0\mingw64\bin\octave-cli-6.4.0.exe" "C:\MatlabScripts\matlabScript1.m" c:\temp\myCuteImage [example for prm]

Matlab fails to run a script when scheduled with Task Scheduler

I am trying to start Matlab and run a script scheduled at a specific time using the windows Task Scheduler.
If I use a scheduled task I can see Matlab starting, but this last fails to load the script and returns me the error below
??? Unexpected Matlab operator.
Do you know what it is and why?
I am using the following syntax
c:\app\matlab\bin\matlab.exe -r c:\MyURL\ScriptFile.m
If I load the script manually and run it it tells me that the file is not in the path so give ms a choice between
Change Current Directory
Add Folder to the Path
Either choices are fine and the script runs fine.
Matlab is starting in its main directory and -r requires your function to be in quotation marks, thats why you get the error.
And you need to change to your workspace first, the syntax is as follows:
matlab -sd pathToYourWorkspace -r "function(parameters)"
Maybe you also want to avoid the complete loading of the whole Matlab working environment, so add at the end:
-nodesktop -nosplash
If you run your task sheduled, are you doing it multiple times? Are you aware that every function call like above opens a new instance of Matlab? This question may be helpful then.
From the comments: of course you could just use the command run to call a script wherever it is.
"run('c:\MyURL\ScriptFile.m')" is an example for "functionName(YourArgs)"
as run is a function and the string 'c:\MyURL\ScriptFile.m' its argument. In this case it is usually not necessary to change the workspace before.

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