problem for brace in array name
1 回表示 (過去 30 日間)
古いコメントを表示
Hello, I have a code as followings.
% Check your directory of SPM
addpath('C:\Users\hyunj\Dropbox\nm_brain_connectivity_tutorial\spm12');
spm;
% Close the pop-up window.
% Check the directory where you saved the dataset
dir_name = 'C:\Users\hyunj\Dropbox\nm_brain_connectivity_tutorial\data';
group_name = {'9110991_NC_cerebnor','9110991_QD_cerebnor','9110991_AD_cerebnor'};
G = 3;
% Load the data set.
X = [];
for g = 1:G
file_name = dir([dir_name group_name{g}]);
count = 1;
for i = 1:length(file_name)
tname = [dir_name group_name{g} '\' file_name(i).name];
if ~isempty(strfind(tname,'img'))
test = spm_vol(tname);
X{g}(:,:,:,count) = spm_read_vols(test);
count = count + 1;
end
end
end
% control data is in X{1}.
size(X{1})
% 91*109*91*11 : 91*109*91 images, 44 subjects
% MCI data is in X{2}
size(X{2})
% 91*109*91*24 : 91*109*91 images, 24 subjects
% AD data is in X{3}
size(X{3})
% 91*109*91*22 : 91*109*91 images, 22 subjects
save 'X.mat' 'X';
I want to save image files into array type.
So, I defined array names using brace indexing.
But, an error occurs after running this code.
Brace indexing is not supported for variables of this type.
Error: test_load_dataset (line 34)
size(X{1})
What should I do? T.T
I am very very beginner for MATLAB. T.T
Please give me hands.
Thanks.
12 件のコメント
Bjorn Gustavsson
2019 年 3 月 15 日
編集済み: Bjorn Gustavsson
2019 年 3 月 15 日
Then that might be because you never have the condition in your if-statement to be true? Check that - that is run your code step by step for g = 1, and see that you actually get what you expect out of that.
回答 (1 件)
Guillaume
2019 年 3 月 15 日
編集済み: Guillaume
2019 年 3 月 15 日
Clearly, if each cell is empty, that's because no file is found. Perhaps you build your path wrong, or perhaps there is no file there. In any case, it's trivial to include a check and a message that warns you if no file is found so at least you can check that you've got the correct path.
If I understood your code correctly, you're looking for any file with img in their name within the group directory. You can ask for these directly with dir avoiding the strfind:
dir_name = 'C:\Users\hyunj\Dropbox\nm_brain_connectivity_tutorial\data';
group_name = {'9110991_NC_cerebnor','9110991_QD_cerebnor','9110991_AD_cerebnor'};
group_vols = cell(1, numel(group_name)); %much better variable name than X!
for g = 1 : numel(group_name)
groupfolder = [dir_name, group_name{g}];
filelist = dir(fullfile(groupfolder, '*img*')); %find all files containing img in groupfolder
if isempty(filelist)
warning('No file found in directory: %s', groupfolder);
else
vols = arrayfun(@(file) spm_read_vols(spm_vol(fullfile(groupfolder, file.name))), filelist, 'UniformOutput', false);
group_vols = cat(4, vols{:});
end
end
Note that instead of growing X{g} in a loop using a count variable, I use arrayfun to generate a cell array that is then converted into a 4D matrix.
2 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!