Azzera filtri
Azzera filtri

How to write all cells of structure in excel sheet ?

3 visualizzazioni (ultimi 30 giorni)
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 Commenti
Stephen23
Stephen23 il 22 Apr 2024
Modificato: Stephen23 il 22 Apr 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).

Accedi per commentare.

Risposta accettata

Stephen23
Stephen23 il 22 Apr 2024
Modificato: Stephen23 il 22 Apr 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}
S = struct with fields:
Theta_03_11_23: {[59.8583] [62.3346] [62.6502] [62.2263]} Theta_04_11_23: {[62.6339] [64.0728] [61.5823]} Theta_10_11_23: {[58.5769] [55.5789] [59.0636] [52.4625] [53.6261] [52.3378]} Theta_04_12_23: {[54.4489] [53.6595] [53.2115] [55.2137] [53.2380] [58.2240]} Theta_06_01_24: {[52.9666] [51.6399] [49.6240] [56.9910] [49.5349] [55.0364]} Theta_12_01_24: {[48.3524] [58.5210] [53.3271] [56.6389] [50.1954] [50.0469]} Theta_06_02_24: {[55.2692] [60.7864] [55.7100] [58.3940] [58.7417] [58.2153]} Theta_17_03_24: {[55.0633] [56.2131] [50.3612] [55.8381] [54.7078] [52.1236]}
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)
ans = 8x7 cell array
{'Theta_03_11_23'} {[59.8583]} {[62.3346]} {[62.6502]} {[ 62.2263]} {[<missing>]} {[<missing>]} {'Theta_04_11_23'} {[62.6339]} {[64.0728]} {[61.5823]} {[<missing>]} {[<missing>]} {[<missing>]} {'Theta_10_11_23'} {[58.5769]} {[55.5789]} {[59.0636]} {[ 52.4625]} {[ 53.6261]} {[ 52.3378]} {'Theta_04_12_23'} {[54.4489]} {[53.6595]} {[53.2115]} {[ 55.2137]} {[ 53.2380]} {[ 58.2240]} {'Theta_06_01_24'} {[52.9666]} {[51.6399]} {[49.6240]} {[ 56.9910]} {[ 49.5349]} {[ 55.0364]} {'Theta_12_01_24'} {[48.3524]} {[58.5210]} {[53.3271]} {[ 56.6389]} {[ 50.1954]} {[ 50.0469]} {'Theta_06_02_24'} {[55.2692]} {[60.7864]} {[55.7100]} {[ 58.3940]} {[ 58.7417]} {[ 58.2153]} {'Theta_17_03_24'} {[55.0633]} {[56.2131]} {[50.3612]} {[ 55.8381]} {[ 54.7078]} {[ 52.1236]}

Più risposte (1)

Harald
Harald il 22 Apr 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

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by