problem for brace in array name
Show older comments
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.
10 Comments
KSSV
on 15 Mar 2019
It seems X is a cell.....can you tell what
whos X
outputs?
Hyunjong Lee
on 15 Mar 2019
KSSV
on 15 Mar 2019
I asked you something else......what
whos X
outputs?
Hyunjong Lee
on 15 Mar 2019
Bjorn Gustavsson
on 15 Mar 2019
Before the look you define X:
X = [];
That creates X as a normal double array with size 0 x 0, then you try to treat X as a cell-array, which fails. Change your initialization to:
X = {};
(Whether necessary or not it might help against workspace cluttering if you want to run multiple similar scripts where X is used as a target variable.)
HTH
"Change your initialization to: X = {};"
That will not resolve the root problem, which is that the code cannot handle the case when the IF is not true and so nothing is allocated to X (the fact the error occurs after the loops tells us this is actually the case). That change will simply mean errors due to accessing X{3}, etc.
A robust solution is to preallocate the cell array correctly:
X = cell(1,G);
and then detect the empty cells after the loop.
Guillaume
on 15 Mar 2019
While we're at it:
group_name = {'9110991_NC_cerebnor','9110991_QD_cerebnor','9110991_AD_cerebnor'};
G = 3;
for g = 1:G
...
So, if you add or remove groups, you need to edit group_name and G and make sure that they match. It's much safer to ask matlab how many groups there are and use that value:
group_name = {'9110991_NC_cerebnor','9110991_QD_cerebnor','9110991_AD_cerebnor'};
for g = 1:numel(group_name)
It's also less lines of code.
KSSV
on 15 Mar 2019
Try this once.
% 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 = cell(G,1);
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 contains(tname,'img')
test = spm_vol(tname);
X{g} = spm_read_vols(test);
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';
Hyunjong Lee
on 15 Mar 2019
Bjorn Gustavsson
on 15 Mar 2019
Edited: Bjorn Gustavsson
on 15 Mar 2019
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.
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!