importing xml and change to mat stuct

5 ビュー (過去 30 日間)
KDRA
KDRA 2018 年 10 月 10 日
コメント済み: Daniel Abraham 2021 年 7 月 15 日
Hello,
I am trying to import multiple *.xml files from specific location and change them into mat structure files but I am facing some problems. This is my code:
if true
% code
end
%loading files from device
filedir= 'X:\GD\EA\ASP\Interest_groups\test\test_export.xml\pilot_studies'; %location of the files
file = dir(fullfile(filedir, '*.xml'));
mfiles=length(file); %number of files in the specified folder
for i = 1: mfiles
fid(i)=fopen(fullfile(filedir, file(i).name),'rt');
s{i}=textscan(fid(i), '%s', 'delimiter','\n');
fclose(fid(i));
end
S = xml2struct(s)
It basically gives me the error in the last line saying that the first input to exist must be a string scalar or a character vector. My s variable is 1x3 cell which is weird. I am not sue how to solve it. I would appreciate your help.
K
  1 件のコメント
Raúl GB
Raúl GB 2018 年 10 月 10 日
編集済み: Raúl GB 2018 年 10 月 10 日
In the last line of the code written by you or the last line of code of any of the functions? Maybe by adding an example of how the xml looks like and the error message we may have a better idea.

サインインしてコメントする。

採用された回答

Guillaume
Guillaume 2018 年 10 月 10 日
You're making your life more complicated than you need to. You don't need to parse the file, xml2struct will do that for you, so the only thing you have to give it is the filename:
S = cell(1, numel(mfiles));
for i = 1 : mfiles
S{i} = xml2struct(fullfile(filedir, file(i).name)); %load xml, convert to structure, and put structure in cell array
end
S = [S{:}]; %concatenate scalar structures into a structure array. Will fail if the xml of the files don't have the same elements.
xml2struct does not take a char array/string as input. The only other possible input is an xml DOM object as returned by xmlread:
S = cell(1, numel(mfiles));
for i = 1 : mfiles
xml = xmlread(fullfile(filedir, file(i).name)); parse xml file
S{i} = xml2struct(xml); %convert xml to structure, and put structure in cell array
end
S = [S{:}]; %concatenate scalar structures into a structure array. Will fail if the xml of the files don't have the same elements.
Obviously, it's simpler to let xml2struct do the call to xmlread for you, so use the first version.
  1 件のコメント
Daniel Abraham
Daniel Abraham 2021 年 7 月 15 日
You're a legend!

サインインしてコメントする。

その他の回答 (1 件)

KDRA
KDRA 2018 年 10 月 10 日
Thanks a lot! I am still learning to write my scripts in the most efficient way ;)

カテゴリ

Help Center および File ExchangeStructured Data and XML Documents についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by