XML-Import and then finding an attribute and saving it in a matrix

4 vues (au cours des 30 derniers jours)
Mueller
Mueller le 12 Juil 2017
Modifié(e) : Mueller le 19 Juil 2017
Dear Matlab Users, I ve spent the hole day on this problem: I imported an XML-File in Matlab. (structure something like that:
  • '<blabla=093 bliu=938d w=0 number=9331 lala= 93882 />'
  • '<blabla=093 bliu=938 w=1 number=9332 lala= 93882 />'
  • '<blabla=093 bliu=93 w=0 number=9333 lala= 93882 />'
  • '<blabla=093 bliu=938 w=1 number=9334 lala= 93882 />'
  • '<blabla=093 bliu=98 w=1 number=9335 lala= 93882 />'
So I got all the rows in cells. I want to to Find and to save every 'number'in a matrix or wherever, where w=0 occurs. In my example it would be the numbers: number=9331 and number=9333. I tried really hard but unfortunately I always get some errors like matrix exceeds dimensions or you cant use this function in cells or whatever (I usually do Image Processing with Matlab and I m not good at working with strings in cells). Anyone who can help or anyone who did wth like that before? Thanks in advance!

Réponses (2)

Prashant Arora
Prashant Arora le 17 Juil 2017
Hi Mueller,
I understand that you would like to obtain numbers from a cell string after finding certain pattern in string. You can use the following method to achieve this:
%Let C be a nx1 Cell array containing the XML strings
Found = contains(C,'w=0');
FoundC = C(Found);
Idx = cellfun(@(x)findstr(x,'number='),FoundC);
Values = arrayfun(@(x,y) str2num(x{:}(y+7:y+10)),FoundC,Idx,'UniformOutput',false);
Values = [Values{:}];
  2 commentaires
Mueller
Mueller le 19 Juil 2017
I will test it- thanks a lot! I coded a bit and I m using this code which works perfectly fine in MATLAB2016b but unfortunately where I use it I only have 2010 version :(
% Name of the xml file
file='name of xml file';
%Load & parse
xml = xml2struct(file);
root=xml.rowdata;
%FInd everything whats in <root>
fields=fieldnames(root);
%initilize constant
c=1;
b=1;
for i=1:numel(fields)
%Check Type
if root.(fields{i}).Attributes.type == '1'
%save if 0
true_id(c)=string(rowdata.(fields{i}).Attributes.id);
c=c+1;
else
false_id(b)=string(rowdata.(fields{i}).Attributes.id);
b=b+1;
end
end
c=1;
double_id=[];
for i=1:length(true_id)
for y=1:length(false_id)
if true_id(i) == false_id(y)
double_id(c)=true_id(i);
c=c+1;
end
end
end
%output true_id false_id double_id
Mueller
Mueller le 19 Juil 2017
Modifié(e) : Mueller le 19 Juil 2017
Now I tried your code. Unfortunately I think in Matlab 2010 there is no 'contains' function. BUT I will get now 2015b. I thinks there should be 'Contain' function.
My Code with your help:
%Let C be a nx1 Cell array containing the XML strings
clear all;
A='abcd.xml';
filetext=fileread(A);
C=regexp (filetext, '/>', 'split');
Found = contains(C,'w="0"');
FoundC = C(Found);
Idx = cellfun(@(x)findstr(x,'number='),FoundC);
Values = arrayfun(@(x,y) str2num(x{:}(y+7:y+10)),FoundC,Idx,'UniformOutput',false);
Values = [Values{:}];

Connectez-vous pour commenter.


Mueller
Mueller le 19 Juil 2017
I found function xml2struct here in matlab forum.

Catégories

En savoir plus sur Argument Definitions dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by