Kambiz
Kambiz

Reputation: 715

MATLAB unix command truncating string

I am trying to use the unix() command in Matlab R2013a to carry out a shell command. The particular command is put together using a series of strings and/or string variables, e.g., unix(['name_of_program --arguments ' string_variables...]); and this particular string is relatively long, but no so long such that it runs when entered in the terminal. However, when using the unix command in Matlab the string is truncated at some limit I cannot figure out, and two commands are issued, i.e., the sub-strings making up the entire command I wish to run. Apart from converting my Matlab script into a shell script, I can't figure another workaround. So before doing that, I'd appreciate any suggestions as how to issue the command in it's entirety in Matlab. Note, that I've also tried creating the command string prior to issuing the unix command, e.g., command = strcat (A,B,C) or command = [A B C] then unix(command) both ending with the same result. In its entirety the command is:

unix(['mne_do_forward_solution --subject ' subjname ...
      ' --src ' sourcespacenames{k} ...
      ' --meas ' datafile ...
      ' --mri ' transname ...
      ' --megonly -all --fwd ' fwdname ...
      ' --overwrite --mindistout rej'])

where mne_do_forward_solution is a C program and everything following -- is an input argument either followed by a value or not. The input argument values, subjname, sourcespacenames{k}, datafile, transname, and fwdname, are all variables in the Matlab workspace of class char.

Upvotes: 0

Views: 615

Answers (1)

Floris
Floris

Reputation: 46415

The most likely cause of your problem is an invisible character in one of your string variables that causes the unix command to treat it as two separate strings. Perhaps there's a stray \r or \0 somewhere? Here is what you do:

myCommand = ['mne_do_forward_solution --subject ' subjname ...
             ' --src ' sourcespacenames{k} ...
             ' --meas ' datafile ...
             ' --mri ' transname ...
             ' --megonly -all --fwd ' fwdname ...
             ' --overwrite --mindistout rej'];

disp(myCommand); % inspect the command string: does it look good?

fprintf(1, '%.0f ', double(myCommand)); % print the ASCII values

unix(myCommand);

Perhaps this will give you some clues about what is going on. It's usually a good idea to create the string you will use as a command outside of the function in which you will use it - it makes this kind of debugging more straightforward.

Upvotes: 2

Related Questions