Running a function with a parameter from the command line

조회 수: 30 (최근 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 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