Running a function with a parameter from the command line

29 次查看(过去 30 天)
I am new to Matlab and realize this is probably a simple question, but I am unable to find a solution online. MATLAB starts, but issues the following error:
/home/ubuntu/code/run_grisli_single.m('/home/ubuntu/my_file.tsv'); exit;
|
Invalid use of operator.
Please let me know what I am doing wrong.
This is my command:
/usr/share/matlab/bin/matlab -nodisplay -nosplash -nodesktop -r "/home/ubuntu/code/run_grisli_single.m('/home/ubuntu/my_file.tsv'); exit;"
This is my script (run_grisli_single.m):
function A = run_grisli_single(varargin)
file = varargin{1}
% compute A;
% return A;
end
  2 个评论
Harsh
Harsh 2024-5-6
Try running the following command
/usr/share/matlab/bin/matlab -nodisplay -nosplash -nodesktop -r "/home/ubuntu/code/run_grisli_single('/home/ubuntu/my_file.tsv'); exit;"

请先登录,再进行评论。

采纳的回答

Steven Lord
Steven Lord 2024-5-6
In general, when you are experiencing difficulties please explain what type of difficulties you're experiencing.
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support so we can investigate.
When you use the -r option, MATLAB runs that code as though you'd typed it in the Command Window. In the Command Window, if you wanted to run a file you wouldn't use the path to the MATLAB code file and you wouldn't use the extension.
If the MATLAB code file you're trying to run is not in a directory accessible to MATLAB (in the current directory or on the MATLAB search path) you'd need to cd to that directory or addpath the directory containing it to the search path.
  3 个评论
Tara
Tara 2024-5-7
Including the path turned out to be the issue. I cd'd to the directory with the code file and ran without specifying the path, and this fixed the problem. Thank you!
Steven Lord
Steven Lord 2024-5-7
If /home/ubuntu/code/ is on the MATLAB search path, run:
/usr/share/matlab/bin/matlab -nodisplay -nosplash -nodesktop -r "run_grisli_single('/home/ubuntu/my_file.tsv'); exit;"
If it's not you'll need to run either:
/usr/share/matlab/bin/matlab -nodisplay -nosplash -nodesktop -r "cd('/home/ubuntu/code/'); run_grisli_single('/home/ubuntu/my_file.tsv'); exit;"
or
/usr/share/matlab/bin/matlab -nodisplay -nosplash -nodesktop -r "addpath('/home/ubuntu/code/'); run_grisli_single('/home/ubuntu/my_file.tsv'); exit;"
Or you could skip the need to call exit and pass in so many startup options by using the -batch option instead of -r. See the description of the two options on this documentation page for more information about what -batch does above and beyond -r.
/usr/share/matlab/bin/matlab -nodisplay -batch "run_grisli_single('/home/ubuntu/my_file.tsv');"

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by