MATLAB Answers

Save as Excel sheet a matlab variable which contains many other variables and cell arrays

79 views (last 30 days)
SNT
SNT on 24 May 2018
Commented: SNT on 31 May 2018
Hi Everyone,
I have a problem as to how I may save a cell array which is contained inside a variable as an excel sheet. I have attached a screen shot. The "ma" is the variable, and I would like to save the cell array "msd" as an excel sheet. Would be great if someone could help me with this, as it is quite complicated for me as this case is a cell array inside another variable... I am looking at both options where I save the cell array as an excel sheet with several tabs (each tab containing each cell data) and the other situation where the excel sheet will have only one sheet, where the cell information will be stored as consecutive sets of columns, one after the other. I don't really know if this is possible...? (Matlab version R2015a)
Thank you in advance!
Best Sara

  0 Comments

Sign in to comment.

Accepted Answer

KSSV
KSSV on 24 May 2018
Edited: KSSV on 24 May 2018
Writes one matrix/ cell in different sheets:
N = 5 ;
A = cell(N,1) ;
for i = 1:N
A{i} = rand(2,3) ;
end
for i = 1:N
xlswrite('check.xlsx',A{i},i)
end
Writes matrix into one sheet, with space between each cell.
N = 5 ;
A = cell(N,1) ;
for i = 1:N
A{i} = rand(2,3) ;
end
B = A ;
[m,n] = size(A{1}) ;
A = cell2mat(A) ;
pos = m+1:m+1:m+(N+1)*m ;
[r,c] = size(A);
add = numel(pos); % How much longer Anew is
Anew = NaN(r + add,c); % Preallocate
idx = setdiff(1:r+add,pos); % all positions of Anew except pos
Anew(idx,:) = A;
xlswrite('check.xlsx',Anew)

  5 Comments

Show 2 older comments
SNT
SNT on 31 May 2018
Hi KSSV,
When I tried the second approach, of saving I get an error at this line. Have I made an error?
Anew(idx,:) = A;
Subscripted assignment dimension mismatch.
Thanks a lot! Sara
SNT
SNT on 31 May 2018
This is the whole code I put on matlab
N=100;
[m,n] = size(ma.msd {1}) ;
A = cell2mat(ma.msd) ;
pos = m+1:m+1:m+(N+1)*m ;
[r,c] = size(ma.msd);
add = numel(pos); % How much longer Anew is
Anew = NaN(r + add,c); % Preallocate
idx = setdiff(1:r+add,pos); % all positions of Anew except pos
Anew(idx,:) = A;
xlswrite('check.xlsx',Anew)
Thanks!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!