0

I am currently writing a script where I have to call MATLAB from the Terminal and pass it a path argument that contains lousy characters.

As expected, I am running into trouble when the path contains spaces / parentheses. Here is a minimal example to illustrate the issue:

matlabcmd="/Applications/MATLAB_R2014b.app/bin/matlab -nodesktop -nosplash" run_dir="Users/Me/My Directory With Spaces (and parentheses)/" $matlabcmd -r "addpath('${run_dir}'),exit"

Some things that I have tried:

  1. Different combinations of double-quotes and single-quotes in the commands above (this usually fails at calling MATLAB).

  2. Using an escape character so that run_dir="Users/Me/My Directory\ With\ Spaces\ \(and parentheses\)/" (this manages to call MATLAB, but then MATLAB gets confused by the spaces... and addpath does not work).

Is there hope?

Berk U.
  • 7,018
  • 6
  • 44
  • 69
  • 1
    Don't try to use a string for the command to execute. It always ends in pain. See http://mywiki.wooledge.org/BashFAQ/050 – Etan Reisner Oct 31 '14 at 14:05
  • It could help to use the DOS path like "Users/Me/MyDire~1/", follow e.g. http://stackoverflow.com/questions/4051088/how-to-get-dos-path-instead-of-windows-path – matheburg Oct 31 '14 at 14:25

1 Answers1

0

Try

matlabcmd="/Applications/MATLAB_R2014b.app/bin/matlab -nodesktop -nosplash" run_dir="Users/Me/My Directory With Spaces (and parentheses)/" $matlabcmd -r "addpath(getenv('run_dir')),exit"

This will prevent the quoting/escaping nightmare and should lead to the desired effect.

damienfrancois
  • 52,978
  • 9
  • 96
  • 110