Giving names to cells

7 Ansichten (letzte 30 Tage)
Ellen
Ellen am 28 Mai 2020
Kommentiert: Ameer Hamza am 31 Mai 2020
Hi!
I am working with multiple arrays which some are split up and other are not. They are stored in a cell type called output.mat. Now I would like to name each cell (also the cells which are located inside cells) based on the names of existing files. I made the following for this:
files = dir('*.roi');
for i=1:length(output)
data = importdata(files(i).name);
chr = files(i).name;
if ~exist(output{i}{i})
save({chr,'.txt'},'output{i}')
else
for ii=1:length(output{i}{i});
save([chr,'_',ii,'.txt'],'output{i}{i}');
end
end
end
However it is not working due to the exist function. The problem is that some cells have cells inside them and others don't. If the cell has a cell inside, I'd like to name them 'filename.1' (and 'filename.2' for the second cell inside the cell and so on). If there is just one cell without an extra cell in it, I'd like to name it 'filename'. So first I'd like to determine if a cell has a cell inside it. I used the exist function for this but obviously it doesn't work. Or do I need to extract all the cells first?
Can someone help me with this?
I apologize if the story is a bit vague.

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 29 Mai 2020
Use iscell() function
if ~iscell(output{i})
Also, note that save() will not accept indexing like this 'output{i}'. You will need to create a temporary variable.
Instead of
save({chr,'.txt'},'output{i}')
write
temp = output{i}
save({chr,'.txt'},'temp') % use appropriate name in place of temp
  2 Kommentare
Ellen
Ellen am 31 Mai 2020
Thanks, this helped a lot!
Ameer Hamza
Ameer Hamza am 31 Mai 2020
I am glad to be of help!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by