How to export a cell array consisting of matrices in each cell into different excel sheets in single workbook?
4 views (last 30 days)
Show older comments
So this is a function which saves transformation matrices, stiffness matrices into cell arrays. Now I want these matrices present in each cell of their respective cell arrays to get exported into an excel sheet. How do I do it? when i use xlswrite the excel sheet is produced but shows different size matrices with different values though some of the element values in matrices are correct but entirely wrong.
function [T,k,K,Kstr]=memdetails(E,A,nodes,mem,tdof,dof)
T=cell(1,length(mem));
k=cell(1,length(mem));
K=cell(1,length(mem));
Kstr=zeros(dof);
for i =1:length(mem)
coord=nodes(mem(i,:),:);
mdof=tdof(i,:);
%coord are member nodal coordinates
x1 = coord(1,1); y1=coord(1,2);
x2 = coord(2,1); y2=coord(2,2);
L = sqrt((x2-x1)^2+(y2-y1)^2);
cos = (x2-x1)/L; sin = (y2-y1)/L;
%Member Transformation matrices
trans = [cos sin 0 0; 0 0 cos sin];
T{1,i}=trans;
%Member local stiffness matrices
localk = E*A/L*[1 -1; -1 1];
k{1,i}=localk;
%Member Global stiffness matrices
globalK = trans'*localk*trans;
K{1,i}=globalK;
%Structure stiffness matrix
Kstr(mdof,mdof)=Kstr(mdof,mdof)+globalK;
end
celldisp({T},'element Tranformation matrix');
celldisp({k},'element local stiffness matrix');
celldisp({K},'element global stiffness matrix');
Kstr
2 Comments
Dyuman Joshi
on 9 Jan 2024
Edited: Dyuman Joshi
on 9 Jan 2024
You should not use built-in function names as names for variables, in this case cos and sin. Rename those variables to something else.
"How do I do it?"
How do you want to store the data in the excel file?
Assuming, E, A and L as scalars, each element is a
> 2x4 array in T
> 2x2 array in k
> 4x4 array in K
What should be the composition of the excel files for each variable? An example would be helpful.
You can store the data in arrays as well, instead of a cell array.
Accepted Answer
Voss
on 10 Jan 2024
"... i want all these matrices in each element of the cell array T to get exported out into an excel workbook which has 3 excel sheets and each sheet has each transformation matrix. And this i want to carry out similarly to local and global stiffness matrices."
filename_T = 'Tranformation matrices.xlsx';
filename_k = 'local stiffness matrices.xlsx';
filename_K = 'global stiffness matrices.xlsx';
for ii = 1:numel(T)
writematrix(T{ii},filename_T,'Sheet',ii);
end
for ii = 1:numel(k)
writematrix(k{ii},filename_k,'Sheet',ii);
end
for ii = 1:numel(K)
writematrix(K{ii},filename_K,'Sheet',ii);
end
2 Comments
Dyuman Joshi
on 10 Jan 2024
@Voss, given that T,k and K are defined in the same loop, you can combine the 3 loops into a single one.
OP can also include the save calls of writematrix() inside the for loop that generate the corresponding data.
More Answers (1)
Harimurali
on 10 Jan 2024
Hi Venkata,
The "writematrix" function in MATLAB can be called inside a loop to export each matrix inside the cell array to seperate worksheets in the same Excel workbook.
Here is the sample MATLAB code to do the same:
cellArr = {ones(5), ones(4,5) .* 4, ones(2,3) .*2} % sample cell array containing matrices in each cell
filename = 'sample.xlsx'; % make sure that the Excel file is in the same path as this MATLAB script
for i = 1:length(cellArr)
writematrix(A{i},filename,'Sheet',i); % each matrix at the ith position in cell array will be written to Sheeti
end
Refer to the following documentation for information about the "writematrix" function:
Hope this helps.
0 Comments
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!