Calling a matlab script with arguments on the linux commandline

I am trying to call a matlab script which takes a single argument (a file path), loads and generates plots from the data. I am hitting the "Error: Invalid use of operator." error, even though this script runs fine within a matlab GUI instance, what am I doing wrong?
(base) bash-4.2$ ~/apps/MATLAB/R2020b/bin/matlab -nosplash -nodesktop -r "~/git/oceanObs/evaluate/make_levels.m('~/Shared/200428_data_OceanObsAnalysis/200913_210216_local_robust_1950_FLRdouble_sptg_200913_79pres1000.mat');"
MATLAB is selecting SOFTWARE OPENGL rendering.
< M A T L A B (R) >
Copyright 1984-2020 The MathWorks, Inc.
R2020b Update 3 (9.9.0.1538559) 64-bit (glnxa64)
November 23, 2020
To get started, type doc.
For product information, visit www.mathworks.com.
/home/duro/git/oceanObs/evaluate/make_levels.m('/home/duro/Shared/200428_data_OceanObsAnalysis/200913_210216_local_robust_1950_FLRdouble_sptg_200913_79pres1000.mat');
|
Error: Invalid use of operator.

3 Comments

Attempting to dig a little deeper, I get this:
(base) bash-4.2$ ~/apps/MATLAB/R2020b/bin/matlab -nosplash -nodesktop -r "make_levels.m /work/duro/Shared/200428_data_OceanObsAnalysis/200913_210216_local_robust_1950_FLRdouble_sptg_200913_79pres1000.mat;"
MATLAB is selecting SOFTWARE OPENGL rendering.
< M A T L A B (R) >
Copyright 1984-2020 The MathWorks, Inc.
R2020b Update 3 (9.9.0.1538559) 64-bit (glnxa64)
November 23, 2020
To get started, type doc.
For product information, visit www.mathworks.com.
make_levels.m /work/duro/Shared/200428_data_OceanObsAnalysis/200913_210216_local_robust_1950_FLRdouble_sptg_200913_79pres1000.mat;
|
Error: Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters.
What character is causing the issue?
Surely, with Matlab 2020b Update 3 you can use underscores (char 95) within directory and file names? Please don't tell me that I have to add a preceding \?
Ok that doesn't seem to be it:
(base) bash-4.2$ ~/apps/MATLAB/R2020b/bin/matlab -nosplash -nodesktop -r "make_levels.m /work/durack1/Shared/200428\_data\_OceanObsAnalysis/200913\_210216\_local\_robust\_1950\_FLRdouble\_sptg\_200913\_79pres1000.mat;"
MATLAB is selecting SOFTWARE OPENGL rendering.
< M A T L A B (R) >
Copyright 1984-2020 The MathWorks, Inc.
R2020b Update 3 (9.9.0.1538559) 64-bit (glnxa64)
November 23, 2020
To get started, type doc.
For product information, visit www.mathworks.com.
make_levels.m /work/durack1/Shared/200428\_data\_OceanObsAnalysis/200913\_210216\_local\_robust\_1950\_FLRdouble\_sptg\_200913\_79pres1000.mat;
|
Error: Invalid text character. Check for unsupported symbol, invisible character, or pasting of non-ASCII characters.

Sign in to comment.

 Accepted Answer

~/apps/MATLAB/R2020b/bin/matlab -nosplash -nodesktop -r "cd('~/git/oceanObs/evaluate');make_levels('~/Shared/200428_data_OceanObsAnalysis/200913_210216_local_robust_1950_FLRdouble_sptg_200913_79pres1000.mat');"
Or better yet,
~/apps/MATLAB/R2020b/bin/matlab -batch "cd('~/git/oceanObs/evaluate');make_levels('~/Shared/200428_data_OceanObsAnalysis/200913_210216_local_robust_1950_FLRdouble_sptg_200913_79pres1000.mat');"

8 Comments

Remember that you cannot name a directory to a function inside MATLAB, that you can only cd to the directory or add the directory to the MATLAB path. Anything that you pass inside -r is going to be passed to the MATLAB command line, just as if inside MATLAB you had given the command
~/git/oceanObs/evaluate/make_levels.m('~/Shared/200428_data_OceanObsAnalysis/200913_210216_local_robust_1950_FLRdouble_sptg_200913_79pres1000.mat');
which attempts to find a ~/ operator which does not exist.
@Walter thanks for that, the first suggestion works, or alternatively navigate to the directory, removing the cd command.
Edit the below does in fact work, without the parallel toolbox license, not sure what tricked this error in a previous iteration
[The second doesn't as -batch requires the parallel toolbox, which is not available on this install - it seems really ridiculous that an additional toolbox license is required for such simple batch functionality to be enabled]
@Walter Roberson just a query, what's going on with this? This is R2020b Update 3 on Linux (RHEL 7.9)
(base) bash-4.2$ ls -al
...
-rwxr-xr-x 1 duro group 11111 Feb 2 22:41 make_AR6.m
...
(base) bash-4.2$ date
Wed Feb 3 10:25:30 PST 2021
(base) bash-4.2$ ~/apps/MATLAB/R2020b/bin/matlab -batch "make_AR6.m" < /dev/null > 210203_1026_make_AR6.log &
[1] 194711
(base) bash-4.2$ Unable to resolve the name make_AR6.m.
EDIT so the issue is that I included a file extension *.m in the command line call.
It seems pretty dumb that including the file extension *.m trips over a 2020 version 9 of software, and the error message doesn't call this out explicitly
Whatever you give as the batch option is eval() -- executed as a command line. It was never the name of a file to execute, it was always a command.
"Execute MATLAB script, statement, or function non-interactively. MATLAB:
Starts without the desktop
Does not display the splash screen
Executes statement
Disables changes to preferences
Disables toolbox caching
Logs text to stdout and stderr
Does not display modal dialog boxes
Exits automatically with exit code 0 if script executes successfully. Otherwise, MATLAB terminates with a non-zero exit code.
statement is MATLAB code enclosed in double quotation marks. If statement is the name of a MATLAB function or script, do not specify the file extension. Any required file must be on the MATLAB search path or in the startup folder.
Use the -batch option in non-interactive scripting or command line work flows. Do not use this option with the -r option.
To test if a session of MATLAB is running in batch mode, call the batchStartupOptionUsed function.
Example: -batch "myscript"
MATLAB does what you tell it to do not what you want it to do. Basically whatever you specify with -r or -batch is run as though you typed it in the Command Window.
Rather than using cd as Walter did consider using the -sd startup option to specify the directory in which MATLAB should start.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!