I have a Matlab .m file that takes three input arguments:
calculate('input.txt','Alex','output.txt')
I would like to run this .m file in a shell script as follows:
matlab -nodisplay -nodesktop -r "run
calculate('input.txt','Alex','output.txt)"
Unfortunately, this did not work. I get the following error:
Error using run (line 70)
calculate('input.txt','Alex','output.txt') not
found.
Any pointer as to how I can give the input arguments/variables?
Thanks.
Note: The following did not work either - complaining too many arguments.
matlab -nodisplay -nodesktop -r "run calculate input.txt Alex output.txt"
I think you just need to remove run. run is for scripts, not for functions (and anyway it's not necessary unless you need to specify the script's path). So, try
matlab -nodisplay -nodesktop -r "calculate('input.txt','Alex','output.txt')"
If your function calculate is not in Matlab's path, change to its folder first. For example
matlab -nodisplay -nodesktop -r "cd 'c:\users\Alex\SomeFolder'; calculate('input.txt','Alex','output.txt')"
Related
is there a way to run a matlab script on the command line and make matlab block until the script is done?
I can invoke a single execution of the script on the command line like so:
"C:\Program Files\MATLAB\R2017a\bin\matlab.exe" -nodisplay -nosplash -nodesktop -r "run('C:\Users\myuser\profile.m');exit;"
There have been several questions on running a script in matlab in command line mode but all of them are about just invoking a single execution via cmd.
They aren't tackling the issue of executing the script multiple times sequentially or executing several scripts in order where one has to finish before the other one is started.
Matlab: Running an m-file from command-line
Open a GUI directly from desktop (Shortcut) in MATLAB environment
If I created a batch file with N repetition of the above command, they will all be started at the same time. Since I'm profiling a set of commands, I'd like them to run on their own. I could also create a script which repeats the current script N times and call this from matlab but I'd rather have matlab process shutdown between invocations so that persistent variables are cleared.
You can use the -wait option above like so:
"C:\Program Files\MATLAB\R2017a\bin\matlab.exe" -wait -nodisplay -nosplash -nodesktop -r "run('C:\Users\myuser\profile.m');exit;"
I've inherited an m-file that calls the function:
function conditions(varargin)
This requires me to type a series of names when running the m-file. E.g.,:
conditions('c01','c02','c03')
I also have a text file (conditions.txt) that contains all conditions names, one conditions per line. e.g.,:
'c01'
'c02'
'c03'
etc...
Is there a way to automate conditions.m through the Bash shell by running through each line of the text file, one line at a time?
Since you're happy to take other suggestions as well, I'm going to suggest you do this through a simple MATLAB script. MATLAB takes a while to start up, so it is quite inefficient to start MATLAB anew for each "condition".
I'm going to assume that your text file example is a simplification, maybe you have multiple parameters per line, and need to pass each of them as an argument to the conditions function. For example the file conditions.txt could contain:
'c01',5,false
'c02',100,true,0,'foo'
...
and you'd want to generate the calls
conditions('c01',5,false)
conditions('c02',100,true,0,'foo')
...
The following code accomplishes this:
f = fopen('conditions.txt','rt');
while true
data = fgetl(f);
if isequal(data,-1), break, end
eval('conditions(',data,')')
end
fclose(f);
exit
You could save that as an M-file script (e.g. runconditions.m), and execute it from bash as follows:
matlab -nosplash -nodesktop -r runconditions
eval is evil, in general, but in this case it's the same as what you'd be doing from the Bash script in Christian's answer. The only difference with that answer is that you avoid starting up MATLAB repeatedly.
At least on windows you can call matlab functions from batch like this: matlab -wait -r 'conditions(%var%)'
So your bash looping should be solvable with a code snipped like this:
while read p; do
matlab -wait -r 'conditions($p)'
done <yourfile.txt
Or any other looping style of your choice:
Looping through the content of a file in Bash
I am trying to run a function on different files; I would like to use Bash-Like script to do that. When I looked on the web; I found that I can use .bat file.
My .bat file contains this
matlab -bodesktop -nosplash -r myFunction('input_1.txt')
matlab -bodesktop -nosplash -r myFunction('input_2.txt')
matlab -bodesktop -nosplash -r myFunction('input_3.txt')
matlab -bodesktop -nosplash -r myFunction('input_4.txt')
matlab -bodesktop -nosplash -r myFunction('input_5.txt')
When I double click the file, it seems that these commands are running on parallel, which, makes the PC to crash.
I looked on Matlab Forum for alternative solutions, but couldn't work with me
Another option I found:
start -wait matlab -bodesktop -nosplash -r "myFunction('input_1.txt');exit"
..
Anyone used this before ?
There are two matlab binaries, one matlabroot/bin the other in matlabroot/bin/win64/. The first one is only a launcher application which typically terminates as soon as the main application is started successfully. To keep it open until the main application terminates you have to use the -wait option with your matlab.exe (not to be confused with the start -wait option, bot can be used together).
In your case try:
matlab -wait -nodesktop -nosplash -r myFunction('input_1.txt')
(I assume you intended to use "nodesktop").
All start parameters for windows are explained here in the documentation. (You have to click "option1...optionN" to expand the relevant section.)
First check that you have the option spelled correctly. Click here for the options.
Try this:
matlab -wait -nodesktop -nosplash -r "myFunction('input_1.txt')"
Edit:
By default, when you call the matlab command from a script, the command starts MATLAB and then immediately executes the next statements in the script. The -wait option pauses the script until MATLAB terminates.
Use the -wait option in a startup script to process the results from MATLAB. Calling MATLAB with this option blocks the script from continuing until the results are generated.
I need to run a MATLAB script inside a bash loop. I'm aware of the syntax which goes as follows:
for i in "${img[#]}"
do
echo $i
matlab -nosplash -nodesktop -nojvm -r "myfunction('$i','cropped_$i');quit;"
done
It works as it should, however, I find it extremely annoying that it has to quit and open MATLAB every iteration, greatly increasing computation time. Is there any way to run it more natively without having to close/open every iteration?
Writing the loop in Matlab is clearly the superior answer, although I don't know Matlab well enough to present that as an answer. Another option is to construct the Matlab code dynamically.
for i in "${img[#]}"; do
code+="myfunction('$i', 'cropped_$i');"
done
code+="quit;"
matlab -nosplash -nodesktop -nojvm -r "$code"
I thought a Makefile was just executing the stated shell commands, but things seem not that simple :
sample.m is a minimal matlab program that displays a word on screen.
When launched from a Makefile, it does not behave the same as when launched from the shell...
From the shell
alex:~$ matlab -nosplash -nodisplay -r "sample"
-> Displays the word correctly
From a Makefile
all :
matlab -nosplash -nodisplay -r "sample"
alex:~$ make
-> Displays the word with a blue bounding box
How can it be different ?
I'm using Matlab 2010a on a Ubuntu 10.04 machine.
No arguments are passed to the sample.m script.
Did you make sure you quit Matlab after running the script?
The Matlab console will linger in memory, invisible when run from make, and some resources will remain locked.
try this in your Makefile:
matlab -nosplash -nodisplay -r "sample; exit"
I tested sample.m and it works here.