How to export a cell array consisting of matrices in each cell into different excel sheets in single workbook?

4 Ansichten (letzte 30 Tage)
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 Kommentare
Dyuman Joshi
Dyuman Joshi am 9 Jan. 2024
Bearbeitet: Dyuman Joshi am 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.
VENKATA SRIHARSHA
VENKATA SRIHARSHA am 10 Jan. 2024
I'm saving the matrices in a cell array as I am using each matrix in each cell/element of respective cell arrays for further calculations. The only way to save an entire matrix into a single element in an array is a cell array. (As a simple array needs a number or string to be as it's element and so it won't take an entire matrix as it's element as it itself is in form of an array.)
Coming to my question, For example I've made a 3 member structure. So the functions loops through 3 members and derives 3 transformation matrices , 3 local and global stiffness matrices. Now first considering the transformation matrices, I'm saving the 3 transformation matrices in a cell array T. so T{1,i} will be a cell array of size 1x3 with each element being a 2x4 matrix.
Now 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. I want to know how to do it.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Voss
Voss am 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 Kommentare
Dyuman Joshi
Dyuman Joshi am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Harimurali
Harimurali am 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.

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by