我有31个科目(S1,S2,S3,S4等)。每一个被试都有三个图像,对比1. and,对比2.and和对比3 and。我想用一个循环将所有被试的所有路径都转换成一个名为P. P的nx1细胞,应该是这样的:
数据/S1/对比1.img 数据/S1/对比2.img 数据/S1/对比3.img 数据/S2/对比1.img 数据/S2/对比2.img 数据/S2/对比3.img。 数据/S31/contast3.img
这就是我尝试过的:
A={'S1','S2','S3',...,'S31'}; % all the subjects
C={'contrast1.img','contrast2.img','contrast3.img'}; % contrast images needed for each subject
P=cell(31*3,1)
for i=1:length(A)
for j=1:length(C)
P{j}=spm_select('FPList', fullfile(data_path, Q{i}) sprintf('%s',cell2mat(C(j)))); % this is to select the three contrast images for each subject. It works in my script. It might not be 100% correct here since I had to simplify for this example.
end
end然而,这只给了我P与最后一个主题的三个对比图像。以前的主题被改写了。这表明循环是错误的,但我不知道如何修复它。有人能帮忙吗?
发布于 2014-04-25 22:56:39
不需要循环。使用ndgrid生成数字的组合,使用左对齐的num2str转换为字符串,使用strcat连接而不使用尾随空格:
M = 31;
N = 3;
[jj ii] = ndgrid(1:N, 1:M);
P = strcat('Data/S',num2str(ii(:),'%-i'),'/contrast',num2str(jj(:),'%-i'),'.img')发布于 2014-04-25 22:19:12
问题是在哪里分配P{j}。
因为j只循环1:3而不关心i,所以您只是重写P{j}的所有三个值。我认为您希望将新值连接到单元格数组中。
for i=1:length(A)
for j=1:length(C)
P ={P; spm_select('FPList', fullfile(data_path, Q{i}) sprintf('%s',cell2mat(C(j))));}
end
end或者您可以直接分配每个值,例如
for i=1:length(A)
for j=1:length(C)
P{3*(i-1)+j} =spm_select('FPList', fullfile(data_path, Q{i}) sprintf('%s',cell2mat(C(j))));
end
end发布于 2014-04-25 22:21:43
我将使用单元格矩阵,它直接表示主题索引和对比索引。
要预先分配使用P=cell(length(A),length(C))并使用P{i,j}=...填充它
当您想访问第五主题的第三张图片时,请使用P{5,3}
https://stackoverflow.com/questions/23303730
复制相似问题