Thread Subject:
Save Matrix as XML in Matlab

Subject: Save Matrix as XML in Matlab

From: smiljan@gmx.de

Date: 11 Aug, 2012 13:14:13

Message: 1 of 19

Hello,

I have a Matrix and i need to save it to xml.

My problem is, that i need to make some adjustements.

I want the first colum to be the DOM and the other ones to be the child.

For example:
1 23 24 25 26 27 27 ;
2 34 35 34 34 34 34 ;
1 23 34 34 34 34 34 ;

Now i need it to be like this:
<1>
<d>23</d>
<l>24</l>
etc.
</1>
<2>
<d>34</d>
<l>35</l>
etc.
</2>

etc.

Can somebody held me with this problem? I don't know how to choose the columns

Subject: Save Matrix as XML in Matlab

From: matlabnewbie

Date: 11 Aug, 2012 13:27:53

Message: 2 of 19

The matrix size can vary , but only the rows. I checked out the whole internet but i dont know how to tell xmlwrite, how to adress my matrix and choose columns.

Subject: Save Matrix as XML in Matlab

From: dpb

Date: 11 Aug, 2012 14:23:52

Message: 3 of 19

On 8/11/2012 8:14 AM, smiljan@gmx.de wrote:
...

> I have a Matrix and i need to save it to xml.

My sympathies...

> My problem is, that i need to make some adjustements.
>
...

> Can somebody held me with this problem? I don't know how to choose the columns

I "know nuthink" about XML per se, but the following link talks about
how to create a DOM in Matlab...

<http://www.mathworks.com/help/techdoc/import_export/f5-86078.html#bsmnj5u>

Looks like you have to build the node structure then populate it.

As for the question of how to chose a given column in Matlab from an
array, see the "Getting Started" section and colon ":" operator...

x(:,1) % the first column of array x (":" is all rows, 1 is column)

--

Subject: Save Matrix as XML in Matlab

From: matlabnewbie

Date: 11 Aug, 2012 17:23:15

Message: 4 of 19

Iam not sure if my post is already there. Kinda confusing here.

Thanks alot for the help. I already checked out the link and i also know that i need to make a node structure. But i dont know how to tell the node structure to use my m atrix and choose a specific column.

I did kinda this:

readtable= get(handles.geometriedatentabelle,'data'); <<<<<<<<getting my matrix

    %1.Create the document node and root element:
docNode = com.mathworks.xml.XMLUtils.createDocument('Geometriedaten'); <<<<this is the DOM
%entry
entry_node = docNode.createElement('Schneckenzone');
entry_node.setNodeValue(readtable(:,1)); <<<<<<<<this should choose my first column...
docNode.getDocumentElement.appendChild(entry_node);
 

Subject: Save Matrix as XML in Matlab

From: matlabnewbie

Date: 11 Aug, 2012 17:14:59

Message: 5 of 19

On Saturday, August 11, 2012 4:23:52 PM UTC+2, dpb wrote:
> On 8/11/2012 8:14 AM, smiljan@gmx.de wrote:
>
> ...
>
>
>
> > I have a Matrix and i need to save it to xml.
>
>
>
> My sympathies...
>
>
>
> > My problem is, that i need to make some adjustements.
>
> >
>
> ...
>
>
>
> > Can somebody held me with this problem? I don't know how to choose the columns
>
>
>
> I "know nuthink" about XML per se, but the following link talks about
>
> how to create a DOM in Matlab...
>
>
>
> <http://www.mathworks.com/help/techdoc/import_export/f5-86078.html#bsmnj5u>
>
>
>
> Looks like you have to build the node structure then populate it.
>
>
>
> As for the question of how to chose a given column in Matlab from an
>
> array, see the "Getting Started" section and colon ":" operator...
>
>
>
> x(:,1) % the first column of array x (":" is all rows, 1 is column)
>
>
>
> --

Thanks for the help. I already looked up the link and still could not figure out how to write down the node structure in terms of implementing my matrix. I know how to choose a row/column but i am not able to write it in 'xml style'.
First I made up a DOM and building up the structure is ok, but telling the structure to use my matrix and get a specific column is kinda confusing for me

Subject: Save Matrix as XML in Matlab

From: dpb

Date: 11 Aug, 2012 18:28:21

Message: 6 of 19

On 8/11/2012 12:14 PM, matlabnewbie wrote:
> On Saturday, August 11, 2012 4:23:52 PM UTC+2, dpb wrote:
...
>> how to create a DOM in Matlab...
>> <http://www.mathworks.com/help/techdoc/import_export/f5-86078.html#bsmnj5u>
>> Looks like you have to build the node structure then populate it.
...

> Thanks for the help. I already looked up the link and still could not
> figure out how to write down the node structure in terms of
> implementing my matrix. I know how to choose a row/column but i am
> not able to write it in 'xml style'. First I made up a DOM and
> building up the structure is ok, but telling the structure to use my
> matrix and get a specific column is kinda confusing for me

Well, I don't know anything other than what the documentation says and
not knowing anything specific about XML structure other than the acronym
all I could do would be to try to work thru from there--maybe come first
of week and activity picks up somebody will see it that actually knows
something specific.

I'd think you would simply use the set() and other methods w/ the
desired subarrays but that's just a guess...

--

Subject: Save Matrix as XML in Matlab

From: dpb

Date: 11 Aug, 2012 22:13:53

Message: 7 of 19

On 8/11/2012 12:23 PM, matlabnewbie wrote:
...

> I did kinda this:
>
> readtable= get(handles.geometriedatentabelle,'data');<<<<<<<<getting my matrix
>
> %1.Create the document node and root element:
> docNode = com.mathworks.xml.XMLUtils.createDocument('Geometriedaten');<<<<this is the DOM
> %entry
> entry_node = docNode.createElement('Schneckenzone');
> entry_node.setNodeValue(readtable(:,1));<<<<<<<<this should choose my first column...
> docNode.getDocumentElement.appendChild(entry_node);


I don't know other than to try to play w/ the DOM stuff and can't do
that here as my release doesn't have it in it...

If your previous posting of what a file should look like is accurate,
then it's easy enough to simply write the stuff out...I copied two lines
from your example data to an array 'l'--

 >> l
l =
      1 23 24 25 26 27 27
      2 34 35 34 34 34 34
 >> for i=1:2
    fprintf('<%d>\n',i);
    fprintf('<d>%d</d>\n',l(i,2));
    fprintf('<l>%d</l>\n',l(i,3:end));
    fprintf('</%d>\n',i);
end
<1>
<d>23</d>
<l>24</l>
<l>25</l>
<l>26</l>
<l>27</l>
<l>27</l>
</1>
<2>
<d>34</d>
<l>35</l>
<l>34</l>
<l>34</l>
<l>34</l>
<l>34</l>
</2>
 >>

Salt to suit...if clever in writing the format strings should be able to
pass the array, I just did this on the fly at the command line in a
couple of tries...

--

Subject: Save Matrix as XML in Matlab

From: dpb

Date: 11 Aug, 2012 22:19:26

Message: 8 of 19

On 8/11/2012 5:13 PM, dpb wrote:
...

> If your previous posting of what a file should look like is accurate,
> then it's easy enough to simply write the stuff out...I copied two lines
> from your example data to an array 'l'--
...
> >> for i=1:2
> fprintf('<%d>\n',i);
> fprintf('<d>%d</d>\n',l(i,2));
> fprintf('<l>%d</l>\n',l(i,3:end));
> fprintf('</%d>\n',i);
> end
...

> ...if clever in writing the format strings should be able to
> pass the array, ...

Dawned on me after I had just hit '<SEND>' it's not even that
clever-needing... :)

 >> fmt=['<%d>\n' '<d>%d</d>\n' repmat('<l>%d</l>\n',1,length(l)-2)
'</%d>\n']
fmt =
<%d>\n<d>%d</d>\n<l>%d</l>\n<l>%d</l>\n<l>%d</l>\n<l>%d</l>\n<l>%d</l>\n</%d>\n
 >> fprintf(fmt,l')
<1>
<d>23</d>
<l>24</l>
<l>25</l>
<l>26</l>
<l>27</l>
<l>27</l>
</2>
<34>
<d>35</d>
<l>34</l>
<l>34</l>
<l>34</l>
<l>34</l>
<l>
 >>

NB you must use transpose l as written because of column-major storage
order in order to output the elements in desired order.

--

Subject: Save Matrix as XML in Matlab

From: dpb

Date: 12 Aug, 2012 00:02:45

Message: 9 of 19

On 8/11/2012 5:19 PM, dpb wrote:
> On 8/11/2012 5:13 PM, dpb wrote:
> ...
>
>> If your previous posting of what a file should look like is accurate,
>> then it's easy enough to simply write the stuff out...I copied two lines
>> from your example data to an array 'l'--
> ...
>> >> for i=1:2
>> fprintf('<%d>\n',i);
>> fprintf('<d>%d</d>\n',l(i,2));
>> fprintf('<l>%d</l>\n',l(i,3:end));
>> fprintf('</%d>\n',i);
>> end
> ...
>
>> ...if clever in writing the format strings should be able to
>> pass the array, ...
>
> Dawned on me after I had just hit '<SEND>' it's not even that
> clever-needing... :)
>
> >> fmt=['<%d>\n' '<d>%d</d>\n' repmat('<l>%d</l>\n',1,length(l)-2)
> '</%d>\n']
> fmt =
> <%d>\n<d>%d</d>\n<l>%d</l>\n<l>%d</l>\n<l>%d</l>\n<l>%d</l>\n<l>%d</l>\n</%d>\n
>
> >> fprintf(fmt,l')
...

Oh, I take that back, sorry--the duplication of the first column to
terminate the section was forgotten above. You need to concatenate the
first column to the end as well...

 > fprintf(fmt,[l l(:,1)]')
<1>
<d>23</d>
<l>24</l>
<l>25</l>
<l>26</l>
<l>27</l>
<l>27</l>
</1>
<2>
<d>34</d>
<l>35</l>
<l>34</l>
<l>34</l>
<l>34</l>
<l>34</l>
</2>
 >>

There... :)

--

Subject: Save Matrix as XML in Matlab

From: matlabnewbie

Date: 12 Aug, 2012 18:39:33

Message: 10 of 19

thanks very much. i did not have time to try it. but ill going to do it now and let you know if i was successful :)

 

Subject: Save Matrix as XML in Matlab

From: dpb

Date: 12 Aug, 2012 20:04:10

Message: 11 of 19

On 8/12/2012 1:39 PM, matlabnewbie wrote:
> thanks very much. i did not have time to try it. but ill going to do
> it now and let you know if i was successful :)

I read the xmlwrite doc and example a little more -- while as noted I
can't test it, from that example it appears you would have to populate
the node element by element using a loop and possibly/probably(?)
num2str() or somesuch to convert to strings as it didn't appear to me in
looking at the reference link xml knows anything about numeric values,
only strings. I'm presuming that, you could always try and see what
happens; all that can go wrong is it either errors and tells you you
screwed up on a call or you don't get what you wanted. If by some
miracle the TMW interface has been vectorized, that would show up to
altho it would seem unlikely they would have gone to such lengths I
suppose anything's possible.

If it's simple formatting and regular like your example, that part at
least I'd deal with as my example. If it's a pita to get whatever
preambles, etc., that you need, you could always do that following the
example w/ xlmwrite and then mush the two pieces together--it is, after
all, only a text file.

--

Subject: Save Matrix as XML in Matlab

From: matlabnewbie

Date: 12 Aug, 2012 20:41:51

Message: 12 of 19

You think a could just write it as your example and save it as an xml? so just make up a text file without dealing with all that xml node problems?

Subject: Save Matrix as XML in Matlab

From: dpb

Date: 12 Aug, 2012 21:37:31

Message: 13 of 19

On 8/12/2012 3:41 PM, matlabnewbie wrote:
> You think a could just write it as your example and save it as an
> xml? so just make up a text file without dealing with all that xml
> node problems?

Sure as long as it meets the spec's, there's nothing magic about the
interface.

It is a UI to the details but there's not that much to what actually
goes into one. The node folderol is simply the bookkeeping to keep
track of the section details--start/stop, etc.

Other way is you write those explicitly. If had a version that had the
function I'd probably do the experiment to see if it knows/understands
about arrays but I'm guessing it doesn't in which case there's probably
as much effort in writing code that way as in doing the formatting as I
did in the example...

--

Subject: Save Matrix as XML in Matlab

From: matlabnewbie

Date: 13 Aug, 2012 10:57:21

Message: 14 of 19


        % Matrix
[filename, pathname] = uiputfile({'*.xml'},'Save as');
 mypath = fullfile(pathname, filename) ;
save(mypath)

fid = fopen(mypath,'wt');
fprintf(fid,'<?xml version=\"1.0\"?>');
fprintf(fid,'<Schneckengeometriedaten>\n');

for i=1:length(readtable)
fprintf(fid,'<%d>\n',readtable(i,1));
fprintf(fid,'<d>%d</d>\n',readtable(i,2));
fprintf(fid,'<l>%d</l>\n',readtable(i,3));
fprintf(fid,'<hs1>%d</hs1>\n',readtable(i,4));
fprintf(fid,'<hs2>%d</hs2>\n',readtable(i,5));
fprintf(fid,'<bs1>%d</bs1>\n',readtable(i,6));
fprintf(fid,'<bs2>%d</bs2>\n',readtable(i,7));
fprintf(fid,'<phi1>%d</phi1>\n',readtable(i,8));
fprintf(fid,'<phi2>%d</phi2>\n',readtable(i,9));
fprintf(fid,'</%d>\n',readtable(i,1));
end
fprintf(fid,'</Schneckengeometriedaten>');
fclose(fid);

this works :) but in the workspace there is still an error message saying that matrix dimensions missmatch
>
you have any idea how i could read the xml textfile and save it to a matrix in the same order i saved it?

Subject: Save Matrix as XML in Matlab

From: dpb

Date: 13 Aug, 2012 13:27:28

Message: 15 of 19

On 8/13/2012 5:57 AM, matlabnewbie wrote:

...[elided example code for brevity]...

> you have any idea how i could read the xml textfile and save it to a
> matrix in the same order i saved it?

That's not as simply to vectorize as your earlier example,
unfortunately. But, if it gets where you're trying to go that's fine...

The simplest way to read in a variable-length text file is into cell
array -- there's an example in textscan() doc of reading an m-file I
believe...oh, no, it's in textread() but the same works in textscan
since textread is now deprecated.

Note to TMW--those examples in textread that aren't in the textscan doc
should be added there if you're really going to remove textread...

2nd note to TMW -- there really, really should also be a facility to
return arrays a la textread() instead of only cells so imo either
textread should not be actually removed or textscan should have the
facility of alternative output. It's going backwards not forwards to
remove that functionality altho there is additional flexibility in
textscan for some purposes granted.

--

Subject: Save Matrix as XML in Matlab

From: matlabnewbie

Date: 13 Aug, 2012 13:41:15

Message: 16 of 19

Thanks for the help.

I tried it with xmlread but i guess it has problems recognizing the file as an xml structure.

I need to check out the textscan function. I already tried out some things, but they all did not work.

Subject: Save Matrix as XML in Matlab

From: dpb

Date: 13 Aug, 2012 14:49:16

Message: 17 of 19

On 8/13/2012 8:41 AM, matlabnewbie wrote:
> Thanks for the help.
>
> I tried it with xmlread but i guess it has problems recognizing the
> file as an xml structure.

I can't test anything w/ xml here...

Did you try to build the example in the xmlwrite() example? If you
could get it to work you could then by comparison perhaps figure out
what went wrong in yours.

--

Subject: Save Matrix as XML in Matlab

From: matlabnewbie

Date: 13 Aug, 2012 19:18:42

Message: 18 of 19

well i did not try it with xmlwrite.
But when i try to parse it with xmlread, it always gets a fatal error message.
I thought this would be easier to handle, since my structure is not very complicated

Subject: Save Matrix as XML in Matlab

From: dpb

Date: 13 Aug, 2012 19:55:30

Message: 19 of 19

On 8/13/2012 2:18 PM, matlabnewbie wrote:
> well i did not try it with xmlwrite.
> But when i try to parse it with xmlread, it always gets a fatal error message.
> I thought this would be easier to handle, since my structure is not very complicated

I meant to do the example in the xlmwrite() documentation/example link
itself as given and verify it actually works if try to read it w/
xlmread().

Assuming it does it ought to get you at least a step closer to debugging
your script if you compare to it...

--

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us