关于shell:MATLAB unix命令截断字符串

MATLAB unix command truncating string

我正在尝试使用Matlab R2013a中的unix()命令来执行shell命令。使用一系列字符串和/或字符串变量(例如unix(['name_of_program --arguments ' string_variables...]))将特定命令组合在一起;这个特定的字符串比较长,但是长度不长到在终端输入时运行。但是,当在Matlab中使用unix命令时,字符串会在无法确定的某个限制处被截断,并且会发出两个命令,即构成我希望运行的整个命令的子字符串。除了将我的Matlab脚本转换为Shell脚本外,我找不到其他解决方法。因此,在进行此操作之前,我希望您能对在Matlab中如何完整地发出命令提出任何建议。请注意,我还曾尝试在发出unix命令之前创建命令字符串,例如command = strcat (A,B,C)command = [A B C]然后unix(command)都以相同的结果结尾。整个命令是:

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

其中mne_do_forward_solution是C程序,并且--之后的所有内容都是输入参数,后跟一个值或不一个值。输入参数值subjnamesourcespacenames{k}datafiletransnamefwdname都是类char的Matlab工作空间中的变量。


最可能的问题原因是字符串变量之一中的不可见字符,导致unix命令将其视为两个单独的字符串。也许某个地方有流浪\
\\0?这是您的工作:

1
2
3
4
5
6
7
8
9
10
11
12
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);

也许这会为您提供一些正在发生的事情的线索。在将要使用的字符串之外的函数中创建要用作命令的字符串通常是一个好主意-这使这种调试更加直接。