Running a function with a parameter from the command line

31 ビュー (過去 30 日間)
Tara
Tara 2024 年 5 月 6 日
コメント済み: Steven Lord 2024 年 5 月 7 日
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 件のコメント
Fangjun Jiang
Fangjun Jiang 2024 年 5 月 6 日
remove .m from your command string
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 Center および File ExchangeStartup and Shutdown についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by