MATLAB Answers

Hayfa R
0

3D Matrix (n*m*p) in Table

Asked by Hayfa R
on 31 Mar 2019
Latest activity Answered by Hayfa R
on 1 Apr 2019
Hello,
I m trying to convert a 3D matrix (n*m*p) in a p* excel sheets with an (n*m) Table in each sheet,
I tried to do it in a for loop but i cant create a 3D Table, and I cant change the variable inside the loop.
Can you please help me. thanks

  1 Comment

dpb
on 31 Mar 2019
Should be no problem at all...show us the code you tried and to ensure we're on the same page what the output from
whos X
where "X" is the variable name of your 3D array.

Sign in to comment.

3 Answers

Answer by Walter Roberson
on 1 Apr 2019
 Accepted Answer

t = YourArray;
tables = cellfun(@array2table,mat2cell(t,size(t,1),size(t,2),ones(1,size(t,3))),'uniform',0);
arrayfun(@(P) writetable(tables{P}, 'YourFileName.xlsx', 'sheet', P, 'writevariablenames', false), 1:size(tables,3), 'uniform', 0);
Or just loop.
for P = 1 : size(YourArray,3)
writetable( array2table(YourArray(:,:,P)), 'YourFileName.xlsx', 'sheet', P, 'writevariablenames', false);
end

  0 Comments

Sign in to comment.


Akira Agata
Answer by Akira Agata
on 1 Apr 2019
 Accepted Answer

How about the following?
% Sample data (n,m,p were assumed to be 20,10,5,respectively)
A = rand(20,10,5);
% Save as an Excel file
for kk = 1:size(A,3)
xlswrite('yourExcel.xlsx',A(:,:,kk),kk);
end

  0 Comments

Sign in to comment.


Answer by Hayfa R
on 1 Apr 2019

It worked well! thank you!

  0 Comments

Sign in to comment.