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 Kommentare

KSSV
KSSV am 15 Mär. 2019
It seems X is a cell.....can you tell what
whos X
outputs?
Hyunjong Lee
Hyunjong Lee am 15 Mär. 2019
It is a group from each dataset.
An output from image files.
KSSV
KSSV am 15 Mär. 2019
I asked you something else......what
whos X
outputs?
Oh sorry
it says as followings.
Name Size Bytes Class Attributes
X 0x0 0 double
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
Stephen23
Stephen23 am 15 Mär. 2019
Bearbeitet: Stephen23 am 17 Sep. 2024
"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.
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
KSSV am 15 Mär. 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';
Thank you for your help.
I ran the code as followings.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Load the dataset.
% You need to download SPM toolbox from
% http://www.fil.ion.ucl.ac.uk/spm/software/
% 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'};
% Load the data set.
X = cell(1,numel(group_name));
for g = 1:numel(group_name)
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');
But I could not get the right answer.
It says,
ans =
0 0
ans =
0 0
ans =
0 0
cap.PNG
There is no value in all cells T.T
Bjorn Gustavsson
Bjorn Gustavsson am 15 Mär. 2019
Bearbeitet: Bjorn Gustavsson am 15 Mär. 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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Guillaume
Guillaume am 15 Mär. 2019
Bearbeitet: Guillaume am 15 Mär. 2019

0 Stimmen

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.

1 Kommentar

Hyunjong Lee
Hyunjong Lee am 16 Mär. 2019
Thank you for all.
Now I've understood what the problem was.
Thank you so much everybody ^^.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Gefragt:

am 15 Mär. 2019

Bearbeitet:

am 17 Sep. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by