Convert a struct to excel file

조회 수: 8 (최근 30일)
Afaf Arfaoui
Afaf Arfaoui 2018년 7월 6일
댓글: Afaf Arfaoui 2018년 7월 6일
I have a struct
The "perSubjectScores" contains two fields
and I want to exract the data in the first one
As you can see the names are not generic, so I find it hard to extract the subject scores for all the rows of the struct "scores"
What is the best solution to do it.
  댓글 수: 1
Jan
Jan 2018년 7월 6일
편집: Jan 2018년 7월 6일
The fieldnames are cruel.
scores(:).perSubjectScores.perSubjectRestrospectiveZScored
This is too redundant to be clear. What about:
scores(:).Subject.RestroZ
?

댓글을 달려면 로그인하십시오.

답변 (1개)

Jan
Jan 2018년 7월 6일
What about a simple loop?
Result = cell(numel(scores), 2);
for k = 1:numel(scores)
F = scores(k).perSubjectScores.perSubjectRestrospectiveZScored;
Result{k, 1} = fieldnames(F);
Result{k, 2} = cell2mat(struct2cell(F));
end
Afterwards a call to cat(1, Result{:,1}) should create a list of all subject names. Unfortunately I'm not really sure, if this works correctly, because I cannot follow the description of your data structure completely. But the general method should get clear and I hope you can adjust it to your needs.
I strongly recommend to simplify the naming scheme. It does not increase the clarity to mention in each level of the nested struct, that the value concern a "scoring". Too exhaustive names for variables or fields decrease the readability and increase the chance of confusion and typos.
  댓글 수: 1
Afaf Arfaoui
Afaf Arfaoui 2018년 7월 6일
Thank you so much it's working!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by