How to write all cells of structure in excel sheet ?
3 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Stephen23
2024 年 4 月 22 日
編集済み: Stephen23
2024 年 4 月 22 日
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).
採用された回答
Stephen23
2024 年 4 月 22 日
編集済み: Stephen23
2024 年 4 月 22 日
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)
その他の回答 (1 件)
Harald
2024 年 4 月 22 日
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
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!