How to export sub/nested structures using writestruct?

조회 수: 24 (최근 30일)
Louis Tomczyk
Louis Tomczyk 2022년 9월 17일
편집: Louis Tomczyk 2022년 9월 20일
Dear all,
Let's say I have structure with 3 sub-strucures such as is also a structure.
I want to export in XML the structures keeping the same tree structure.
I started to write a function:
function export_structure(input_structure)
% 0/ input structure informations
struct_name = inputname(1);
names = fieldnames(input_structure);
nfields = length(names);
% 1/ initialisation to find sub structures
count_substructs= 0;
substruct_names = string([1:nfields]);
% 2/ find the sub structures' names
for k = 1:nfields
if isstruct(input_structure.(names{k})) == 1
count_substructs = count_substructs+1;
substruct_names(k) = names{k};
end
end
% 3/ remove the extra sub structures added by the initialisation
if count_substructs ~= 0
substruct_names = rmfield(substruct_names,string([count_substructs:nfields]));
end
% 4/ export all the structures
for k = 1:count_substructs+1
if isstruct(input_structure.(names{k})) == 1
export_parameters(input_structure.(substruct_names{k}));
else
writestruct(input_structure, ...
strcat(string(datetime),sprintf(...
"--- %s parameters.xml",struct_name)));
end
end
It seems to work when I don't have sub-structures.
However when I have it does not.
Indeed, during my initialisation step, step 1, I create an array of strings of length which is the maximum number of sub-structs in the input.
Then I try to find the sub-structs names, step 2.
But I don't manage to check if the field of a struct is a stuct or not.
Any ideas?
Thanks a lot.
Best,
louis

채택된 답변

Chris
Chris 2022년 9월 20일
편집: Chris 2022년 9월 20일
If you want to write to a single output file, writestruct() already supports nested structs containing scalar or vector values.
If you are experiencing issues, it could be that a field contains a value that isn't a scalar or vector, or that the filename is not valid.
If you want a separate file for each struct, or to write a function that allows other kinds of values, the end of our other answer thread might be a good place to start.

추가 답변 (1개)

Chris
Chris 2022년 9월 18일
편집: Chris 2022년 9월 18일
In step 3, you are trying to do an rmfield operation on a string array (rmfield is for structs only). If you want to remove the strings that don't correspond to a substruct, you could initialize like:
substruct_names = strings(3,1);
and remove the empty strings in step 3 like:
substruct_names(substruct_names = '') = [];
But then the logic in step 4 doesn't quite work yet.
  댓글 수: 6
Louis Tomczyk
Louis Tomczyk 2022년 9월 20일
Dear @Chris,
Actually I am ashamed of what is going to follow and please forgive me...
The function is already enough and does the job.
I still don't understand how I didn't see it before as it was explicitely showed in the documentation: https://fr.mathworks.com/help/matlab/ref/writestruct.html
thanks and sorry again.
best,
louis
ps. maybe this question could be removed from the page by the moderators...
Chris
Chris 2022년 9월 20일
편집: Chris 2022년 9월 20일
Or you could accept my other answer, since I work for internet points. :)
We both learned something that others may want to know. I think that makes this page worth keeping around. (You could edit the initial question for clarity, if you wanted).

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

카테고리

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

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by