saving a figure within a loop

1 次查看(过去 30 天)
Victoria Klimaj
Victoria Klimaj 2013-10-13
Hi,
I am trying to use the script below to generate a figure based on a text files for each one of 155 subject folders. The for loop is meant to change the subject folder every time and then generate the figure based on the text file located within it and then save the output in the same subject folder.
For some reason I am getting "index exceeds matrix dimension" errors on the lines that start with "subplot" when I try to run this in a for loop (though this portion of the script works fine when it is just handling one subject outside a loop), as well as an "invalid handle" error while evaluating the "saveas" line. Any thoughts on how I could make it work? (a note--the subject folders are specified in the Order.mat file)
Thanks! -Victoria
%%%%%
nsubj=155
location='/Volumes/USB_AIB/MOVr1unzipDONE'
load /Volumes/USB_AIB/Order.mat
for csubj=1:nsubj
rp = cellstr(spm_select('FPList',[location,filesep,Order{csubj,1}], 'rp_af*.txt'));
h = figure;
subplot(2,1,1);plot(rp(:,1:3));
set(gca,'xlim',[0 size(rp,1)+1]);
subplot(2,1,2);plot(rp(:,4:6));
set(gca,'xlim',[0 size(rp,1)+1]);
my_saving_dir = cellstr([location,filesep,Order{csubj,1}]); % this is folder to save to
my_file_name = 'headmotion'; % feel free to choose another extension
saving_name = strcat(my_saving_dir,filesep,my_file_name);
saveas(h,saving_name)
end|

回答(1 个)

Walter Roberson
Walter Roberson 2013-10-13
If your rp is a cellstr() result, then it is a cell array. So rp(:,1:3) if it exists at all would be a cell array of strings. And you are trying to plot() that cell array.
Remember, cellstr() does not read data whose filenames are contained in the strings, and even if spm_select() is doing the reading, you cannot plot() a cell array.
  1 个评论
Victoria Klimaj
Victoria Klimaj 2013-10-14
Thanks! I tried rewriting the line as follows--no longer a cell array result, just trying to select the file. However, I having trouble with this too.
rp=spm_select('FPList',fullfile(location,filesep,Order{csubj,1}),'^rp*');
Originally, the script was able to plot an individual subject's file when it was not part of a for loop and the rp file was hand-chosen using the line "spm_select(spm_load)" instead of the line above. Any reason why that would work, but the line above does not?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by