How do I pass a shell script variable as an argument to a matlab function?

43 ビュー (過去 30 日間)
I have a function 'main.m' that I want to accept a file path as input in a shell script.
P=/path/to/file.csv
matlab -r main($P)
or simply:
matlab -r main('/path/to/file.csv')
I have 'main.m' written as a function with no output. e.g.:
function main(filePath)
% code
end
I'm not sure if there are errors in the function declaration, the script syntax or both for this purpose. Any guidance is appreciated! Running MATLAB R2017a
  2 件のコメント
Geoff Hayes
Geoff Hayes 2018 年 3 月 15 日
Matthew - what is your operating system? On my Mac, I need to enclose the statement (that I wish to execute) in double quotes
/Applications/MATLAB_R2014a.app/bin/matlab -nodisplay -r "main('hello')"
Do you need to do the same? If you are still observing an error then please copy and paste the full error message to this question.
Matthew Thompson
Matthew Thompson 2018 年 3 月 15 日
Actually that did exactly what I needed! I'm using Linux. I didn't try double quotes before because I must have misunderstood the doc page on command line input saying to exclude double quotes on MATLAB functions or scripts. Here's my final working shell code:
P=\'/path/to/file.csv\'
matlab -nodisplay -r "main($P)"

サインインしてコメントする。

採用された回答

Geoff Hayes
Geoff Hayes 2018 年 3 月 16 日
The statement being passed to matlab needs to be in double quotes
/Applications/MATLAB_R2014a.app/bin/matlab -nodisplay -r "main('hello')"
or
matlab -nodisplay -r "main($P)"
  3 件のコメント
Geoff Hayes
Geoff Hayes 2018 年 3 月 16 日
Hmmm...perhaps this is for newer versions of MATLAB (the documentation is for R2018a). My version, R2014a, says
matlab -r "command" starts MATLAB and executes the specified MATLAB command. Include the command in double quotation marks ("command"). If command is the name of a MATLAB function or script, do not specify the file extension. To separate multiple statements, use semicolons or commas.
So perhaps newer versions don't require the double quotes. Which version of MATLAB are you using?
Matthew Thompson
Matthew Thompson 2018 年 3 月 16 日
Ah, that's a good point. I didn't see that it was for R2018a. I'm using R2017a but will remember that if I download the update.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by