MATLAB: How tomport Parameters from XML file into Matlab Workspace


I have XML file similar to follows:
I want to import parameters listed in above xml file to Matlab base workspace as follows:
How it can be achieved using Matlab Script? And above functionality can be achieved using pushbutton in GUI?

Best Answer

xmldoc = xmlread( filename );
x0 = str2double( xmldoc.getElementsByTagName( 'x0' ).item( 0 ).getFirstChild.getNodeValue( ) );
x1 = str2double( xmldoc.getElementsByTagName( 'x1' ).item( 0 ).getFirstChild.getNodeValue( ) );
x2 = str2double( xmldoc.getElementsByTagName( 'x2' ).item( 0 ).getFirstChild.getNodeValue( ) );
works if that is as complicated as your file gets. If you have x0 etc in other places too then you'd need to find specifically the 'states' node first.
There may be simpler ways, but I always get lost in the functions for reading XML - they never seem at all intuitive or easy to debug. Generally I split into more lines with intermediate variables.
Also I would read those into a single vector x as x(1), x(2), x(3), but the above does what you asked.