Clear Filters
Clear Filters

problem using writecell (Nested cell arrays are not supported.)

18 views (last 30 days)
i'm writing a code to import contrast data (in form of cells) but somehow it's returning an error in this particular code.
mainfolder ='/Users/sesiliamaidelin/Desktop/uni/3 year 3/dissertation/Sesilia copy';
%% extract images
% change here to change folder numbers
%ori = '/Users/sesiliamaidelin/Desktop/uni/3 year 3/dissertation/Sesilia copy/73/';
ori = uigetdir();
filesAndFolders = dir([ori '/**']);
files_ = {filesAndFolders.name};
pngidx = cellfun(@(x) contains(x,'.png'),files_); % Index to extract dicom files only
pnglist = files_(pngidx); % List of dicom files only
%%
eedn = '/Users/sesiliamaidelin/Desktop/uni/3 year 3/dissertation/Sesilia copy/73_EEDN/';
filesAndFolders_e = dir([eedn '/**']);
files_e = {filesAndFolders_e.name};
pngidx_e = cellfun(@(x) contains(x,'.png'),files_e); % Index to extract dicom files only
pnglist_e = files_e(pngidx_e);
uddn = '/Users/sesiliamaidelin/Desktop/uni/3 year 3/dissertation/Sesilia copy/73_UDDN/';
filesAndFolders_u = dir([uddn '/**']);
files_u = {filesAndFolders_u.name};
pngidx_u = cellfun(@(x) contains(x,'.png'),files_u); % Index to extract dicom files only
pnglist_u = files_u(pngidx_u);
exceldata = {'Frame in 73','contrast preprocessing','contrast eedn','contrast uddn'}; % excel header
%%
% will start with 5 as that is when images stabilise
for ii = 5% :numel(pnglist)
% measure ori image
name = pnglist(ii);
nem = char(append(ori,'/',name));
im = imread(nem);
imshow(im);
[ROI,rect] = imcrop(im);
[mean_i1,contrast_i1,std_i1] = im_stats(ROI);
% measure eedn image
nem_e = char(append(eedn,pnglist_e(ii)));
im_e = imread(nem_e);
e_ROI = imcrop(im_e,rect);
[mean_e, contrast_e, std_e] = im_stats(e_ROI);
%measure uddn image
nem_u = char(append(uddn,pnglist_u(ii)));
im_u = imread(nem_u);
u_ROI = imcrop(im_u,rect);
[mean_u, contrast_u, std_u] = im_stats(u_ROI);
% put together into an excel spreadsheet
data = horzcat({name}, {contrast_i1}, {contrast_e}, {contrast_u});
exceldata = vertcat(exceldata, data);
end
writecell(exceldata,'contrast.xlsx','UseExcel',true,'Sheet','Extra','WriteMode','append')
error message :
Error using writecell (line 153)
Nested cell arrays are not supported.
Error in contrast (line 58)
writecell(exceldata,'contrast.xlsx','UseExcel',true,'Sheet','Extra','WriteMode','append')
  1 Comment
Shivam
Shivam on 27 Sep 2023
Can you provide the files you are using in the script? I will check back in 24 hours.
Thanks.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 27 Sep 2023
Edited: Guillaume on 27 Sep 2023
Hello,
I did not execute your code but your error is probably related to this :
name = pnglist(ii);
pnglist appears to be a cell array, so pnglist(ii) is also a cell. Then you concatenate this in exceldata and you get a nested cell.
This is why you need to use char(...) everytime you use your variables, this is probably not what you want to do.
Solution : you should use curly brace indexing for all your cells (pnglist{ii}, pnglist_e{ii}, pnglist_u{ii}). This way you can also remove the call to function char(...) that are not needed.
If I can add an advice : the way you look for file extension is not optimal. You could use directly the dir function with the correct extension, like:
filesAndFolders_u = dir([uddn '/**/*.png']);
Then you don't need anymore to check for the file extension.
Please note also that the string functions like contains, endsWith, strcmp, accept cell array as input. You don't have to use cellfun for this functions.
Basically your code shoiuld look like this :
mainfolder ='/Users/sesiliamaidelin/Desktop/uni/3 year 3/dissertation/Sesilia copy';
%% extract images
% change here to change folder numbers
%ori = '/Users/sesiliamaidelin/Desktop/uni/3 year 3/dissertation/Sesilia copy/73/';
ori = uigetdir();
pnglist = dir(fullfile(ori,'**', '*.png')); % List of dicom files only
%%
eedn = '/Users/sesiliamaidelin/Desktop/uni/3 year 3/dissertation/Sesilia copy/73_EEDN/';
pnglist_e = dir(fullfile(eedn,'**', '*.png'));
uddn = '/Users/sesiliamaidelin/Desktop/uni/3 year 3/dissertation/Sesilia copy/73_UDDN/';
pnglist_u = dir(fullfile(uddn,'**', '*.png'));
exceldata = {'Frame in 73','contrast preprocessing','contrast eedn','contrast uddn'}; % excel header
%%
% will start with 5 as that is when images stabilise
for ii = 5% :numel(pnglist)
% measure ori image
im = imread(fullfile(pnglist(ii).folder, pnglist(ii).name));
imshow(im);
[ROI,rect] = imcrop(im);
[mean_i1,contrast_i1,std_i1] = im_stats(ROI);
% measure eedn image
im_e = imread(fullfile(pnglist_e(ii).folder, pnglist_e(ii).name));
e_ROI = imcrop(im_e,rect);
[mean_e, contrast_e, std_e] = im_stats(e_ROI);
%measure uddn image
im_u = imread(fullfile(pnglist_u(ii).folder, pnglist_u(ii).name));
u_ROI = imcrop(im_u,rect);
[mean_u, contrast_u, std_u] = im_stats(u_ROI);
% put together into an excel spreadsheet
data = horzcat({pnglist(ii).name}, {contrast_i1}, {contrast_e}, {contrast_u});
exceldata = vertcat(exceldata, data);
end
writecell(exceldata,'contrast.xlsx','UseExcel',true,'Sheet','Extra','WriteMode','append')

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!