creating matrix from for loop with system command output

조회 수: 2 (최근 30일)
Sean
Sean 2012년 2월 7일
Hi All,
I would greatly appreciate some help on this.
I am trying to do the following.
Within a for loop, run 2 system commands and have the ouputs from those two commands make up all three elements of one row of a matrix for each iteration of the loop. I would then like to print the entire matrix to an excel file.
I tried this:
for i=1:3,
command1 = ['fslmeants -i image1 -m example_' num2str(i) '.nii.gz' ]
C(i,1) = evalc('system([command1])')
command2 = ['fslstats example_' num2str(i) ' -V']
C(i,2:3) = evalc('system ([command2])')
end
I received the following error message:
??? Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in ==> name_of_script at 8 C(i,1) = evalc('system ([command1])')
I then tried this
for i=1:3,
command1 = ['fslmeants -i image1 -m example_' num2str(i) '.nii.gz' ]
A = evalc('system([command1])')
command2 = ['fslstats example_' num2str(i) ' -V']
B = evalc('system ([command2)')
end
the output for system ([command1]) when I don't try to assign a matrix element to it is
A =
0.302714 ans =
0
the output for system ([command2]) when I don't try to assign matrix elements to it is
B =
1413 19506.814453
ans =
0
If I then type B(1,2) I get
ans =
4
which gives me the second digit of the first number in B, not the second number (19506.814453) as I wanted.
I then checked if B was a character array as follows:
ischar B
ans =
1
So I guess if I can find a way for matlab to recognise the output of system([command1]) and system ([command2]) as 3 elements of the same row for each iteration of the for loop I'd be sorted.
So ideally, the first row of the Matrix C would be
C(1,:) = 0.302714 1413 19506.814453
and an extra row would be added with each iteration of the for loop.
Can anyone enlighten a lost soul?
All help much appreciated,
Best wishes,
Sean

답변 (1개)

Walter Roberson
Walter Roberson 2012년 2월 7일
B = evalc('system([command2])');
B = regexp(B,' +','split');
B{2} will now be the second column.

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by