How to write all cells of structure in excel sheet ?
Show older comments
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 Comments
Anjaneyulu Bairi
on 22 Apr 2024
Hi,
Can you attach that file here?
Ahmad Al-Issa
on 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).
Ahmad Al-Issa
on 22 Apr 2024
Accepted Answer
More Answers (1)
Harald
on 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
1 Comment
Ahmad Al-Issa
on 22 Apr 2024
Categories
Find more on Text Files 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!