Undefined function or variable when passing arguments from command line

3 ビュー (過去 30 日間)
LD
LD 2018 年 7 月 23 日
コメント済み: LD 2018 年 7 月 26 日
I'm facing problems with using my bash variables inside a matlab function that I call from the command line. I'm fairy new to Matlab and haven't been able to solve this at all. I define my variables in bash and pass them to a function call like so:
/opt/MATLAB/bin/matlab -r "try; matTest('n1=$N1', 'n2=$N2', 'jobID=$SLURM_ID', 'outfile=$outfile'); catch; end;"
And my matlab function starts out like this:
function matTest(n1, n2, jobID, outfile)
infile=strcat(jobID,'-Ztemp.csv');
infile
n1
n2
outfile
Where I test out that everything was read correctly, which it was. However when I'm still in my matlab shell, and I try to print n1 or outfile again it gives me the error: Undefined function or variable 'n2'.
Which in turns prevents me from calling another function (in the same path) that takes these parameters. What is the problem here and how can I solve it please? Thanks!

回答 (1 件)

Geoff Hayes
Geoff Hayes 2018 年 7 月 23 日
LD - when I'm still in my matlab shell, and I try to print n1 or outfile again it gives me the error: Undefined function or variable 'n2'. This is because the scope of these variables is limited to the function in which they are defined and so are NOT available/visible outside of the function.
If you wish to access these variables outside of the function, then you would need to set them as output parameters in your function signature.
  1 件のコメント
LD
LD 2018 年 7 月 26 日
Hey thanks for the feedback! I did get what you're saying but it turned out to be way simpler.. I was passing the variables quoted which I shouldn't have, especially since the first three are integers. So, it was solved by changing the call to:
/opt/MATLAB/bin/matlab -r "try; matTest($N1, $N2, $SLURM_ID, '$outfile'); catch; end;"

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

カテゴリ

Help Center および File ExchangeEntering Commands についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by