How do I push back into a struct a list of XML files processed using "xml2struct"?
조회 수: 11 (최근 30일)
이전 댓글 표시
MathWorks Support Team
2021년 2월 16일
답변: MathWorks Support Team
2021년 2월 23일
I have a list of XML files that I want to read and push back into a struct using "xml2struct". How do I do this dynamically so that the size of struct grows in a controlled way each time an XML file is converted and added?
채택된 답변
MathWorks Support Team
2021년 2월 16일
One way to achieve this is to read the list of XML files in a "while" loop and populate a nested structure. A nested structure is quite natural for collating many similarly structured XML files as each one itself can be thought of as a struct of structures. The Data Import and Export functionality in MATLAB allows the user to check if they have reached the end of a file while reading it. This means the list of source files can be read in MATLAB and the "xml2struct" function executed on each filename. For example,
fid = fopen('list.txt','rt'); % list of xml files to convert
s = struct;
n = 1;
while ~feof(fid) % checks if at end of file
filename = fgetl(fid); % get the filename line-by-line
s.input_file(n) = xml2struct(filename);
n = n + 1; % iterate in the struct
end
fclose(fid);
In this example, the struct constructed grows in size on each iteration of the while loop as necessary, which means the memory allocation dynamically grows. This can be checked by calling "whos s" on each loop iteration.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Workspace Variables and MAT Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!