run function from command line

조회 수: 57 (최근 30일)
Simon Michel
Simon Michel 2017년 2월 27일
편집: Walter Roberson 2025년 9월 12일
I want to start a matlab function from the unix command line. For now I use
matlab -nodisplay -r "functionname(argument1, argument2, argumentN);exit"
But for this the function I call needs to be in the folder where I am. Is there a way to call the function like
matlab -nodisplay -r "/path/to/functionname(argument1, argument2, argumentN);exit"
without manually adding it to the search path of matlab?

채택된 답변

Jan
Jan 2017년 2월 27일
Do you mean:
matlab -nodisplay -r "cd('/path/to'); functionname(argument1, argument2, argumentN);exit"
  댓글 수: 4
Jhe Mag
Jhe Mag 2022년 1월 16일
편집: Jhe Mag 2022년 1월 16일
Thank you Mr. Jan. Seems like windows prompt is slightly the same with unix haha.
Someone might be needing this: For windows command prompt, I used the command : "C:\Program Files\Polyspace\R2021a\bin\matlab.exe" -nosplash -nodesktop -r "cd('D:\matlab_project\scripts\'), testFnc('blah'), exit"
with my testFnc.m
function sample = testFnc(stri)
sample = stri
disp(sample)
end
Jon
Jon 2025년 9월 11일
FWIW, this does seem to cd back to the original directory after MATLAB exits

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2025년 9월 12일
I'm not 100% sure this option was available in releases R2017a or R2017b, but I'm pretty sure it was. Use the -sd startup option to cause MATLAB to start in a certain directory.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by