Calling Matlab from linux terminal

3 views (last 30 days)
Khushboo Punjabi
Khushboo Punjabi on 10 Feb 2022
Commented: Khushboo Punjabi on 10 Feb 2022
I'm writing a bash script which calls a Matlab function in one of the steps. Two images generated in the previous steps are inputs to this function. I have set variable names for the paths to both these images and use thefollowing command.
Image1Path="PathToImage1"
Image2Path="PathToImage"
matlab -nojvm -nodesktop -r "try; cd('pwd'); function_name(' "$Image1Path" ', ' "$Image2Path" '); catch; end; quit"
When I run it in the terminal, I get the following error. I think the problem is that it is getting the paths without the ' '/ " ", but I'm not sure. Any help is appreciated.
try; cd(pwd); presurfer_example_pt(/media/../../../../../../image1.nii.gz, /media../../../../../../image2.nii); catch; end; quit
|
Error: Invalid use of operator.
  2 Comments
Walter Roberson
Walter Roberson on 10 Feb 2022
That does not make sense if those are your commands. You have the 'pwd' inside the "" level, so the apostrophes should become part of the command, and you should be ending up with
try; cd('pwd'); presurfer_example_pt('/media/../../../../../../image1.nii.gz', '/media../../../../../../image2.nii'); catch; end; quit
.. Though your command should probably be
matlab -nojvm -nodesktop -r "try; cd('$PWD'); function_name('$Image1Path', '$Image2Path'); catch; end; quit"
The rule for shells is that inside double-quotes, un-escaped $ sequences are replaced even inside of ' ' quotes. For example,
echo "cd($PWD); disp('$PWD');"
might evaluate to echoing
cd(/Applications/MATLAB_R2016a.app); disp('/Applications/MATLAB_R2016a.app');
... Notice the lack of '' around the path the first time... because the '' were not included in the code. And notice that the '' were retained everywhere they were coded inside double-quotes.
The other rule for shells is that if you are inside (unnested) single-quotes then no variable interpolation is done
For you to have lost the '' then either you are not showing us the actual commands you are using, or else there is another evaluation level that is happening.
The $ interpolation that is being done in the code you posted involves the coder having deliberately removed all levels of quoting -- which would not be something you would normally do. There are times when you need to switch from single-quotes (no variable interpolation) to wanting variable interpolation, but the sequence for doing so would typically look like
'sequence of $uninterpolated stuff'"$interpolated_variable"'back to uninterpolated here'
Khushboo Punjabi
Khushboo Punjabi on 10 Feb 2022
Thanks Walter for your help, your suggested command worked. Also, with your explaination I now better understand these rules.

Sign in to comment.

Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!