How to write all cells of structure in excel sheet ?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Ahmad Al-Issa
el 22 de Abr. de 2024
Comentada: Ahmad Al-Issa
el 22 de Abr. de 2024
Hello all,
I have variable which has 1x1 structure, and this structure has many field (it can be reached to 200). each field has has diffrent size of cells as explained in the images.
I want to write the results in excel sheel. like in the uploaded image:
my code is:
writematrix ('Day','Results_Theta.xlsx','WriteMode','replacefile','Sheet','Theta','Range','A1');
writematrix ('Theta 1','Results_Theta.xlsx','Sheet','Theta','Range','B1');
writematrix ('Theta 2','Results_Theta.xlsx','Sheet','Theta','Range','C1');
writematrix ('Theta 3','Results_Theta.xlsx','Sheet','Theta','Range','D1');
writematrix ('Theta 4','Results_Theta.xlsx','Sheet','Theta','Range','E1');
writematrix ('Theta 5','Results_Theta.xlsx','Sheet','Theta','Range','F1');
writematrix ('Theta 6','Results_Theta.xlsx','Sheet','Theta','Range','G1');
writematrix ('Theta 7','Results_Theta.xlsx','Sheet','Theta','Range','H1');
So, how can I write the results?
4 comentarios
Stephen23
el 22 de Abr. de 2024
Editada: Stephen23
el 22 de Abr. de 2024
Please upload the structure in MAT file.
If you have a chance to redesign that data: use a non-scalar structure with one field of the date, one for the data, and fields for anything else that you want. Avoid forcing meta-data (e.g. dates) into fieldnames (which makes it harder to process your data).
Respuesta aceptada
Stephen23
el 22 de Abr. de 2024
Editada: Stephen23
el 22 de Abr. de 2024
Why is a scalar structure nested inside a (pointless) scalar cell array?
Why are lots of scalar numerics stored inside a cell array (rather than simply in numeric arrays) ?
The nested nested data design is complex and inefficient: one non-scalar structure would be much better.
Perhaps this does what you want:
S = load('Results_Theta_Day.mat').Results_Theta_Day{1}
C = fieldnames(S);
F = 'Results_Theta.xlsx';
writecell(C,F,'WriteMode','replacefile','Sheet','Theta','Range','A2');
for k = 1:numel(C)
A = cell2mat(S.(C{k}));
writematrix(A,F,'Sheet','Theta','Range',sprintf('B%u',1+k))
end
Checking:
readcell(F)
Más respuestas (1)
Harald
el 22 de Abr. de 2024
Hi,
you need to either extract the data you need to write or convert it to a format that allows writing everything in one go. For performance reasons, the latter is preferable.Here is a shot at it:
s = Results_Theta_Day{1};
c = struct2cell(s);
el = structfun(@length, s)
maxEl = max(el);
data = cell(length(el), maxEl);
for k = 1:length(c)
data(k,1:el(k)) = c{k};
end
data = [fieldnames(s), data]; % optional
writecell(data,'Results_Theta.xlsx','Sheet','Theta','Range','B1');
Best wishes,
Harald
Ver también
Categorías
Más información sobre Spreadsheets en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!