MATLAB: How to create a simulink block that reads from an XML file that contains initial state conditions in Simulink 8.0 (R2012b)

domMATLABsimulinkxml

My purpose is to modify the initial conditions for my algorithm in an XML file and then test it in Simulink. I can then modify the XML file until I get the results I desire in Simulink. This then allows me to take the XML file and use it in my other software package without having to write new code.

Best Answer

There is not a built-in block for reading XML files into Simulink 8.0 (R2012b). However, there are tools that will allow you to create your own block to do as you desire.
One of the most straight forward ways to build your own block is to use the Level-2 MATLAB S-Function block. This block allows you to write MATLAB code, a S-Function, to define the characteristics and functionality of your block such as the number of inputs, the number of outputs and how the outputs are calculated.
In the MATLAB S-Function, you can write code to read and parse an XML file. Fortunately, there exists the MATLAB function XMLREAD which you can use to read an XML file into a Document Object Model (DOM) node. You will then need to write your own code to parse the DOM node and extract the information you need.
However, if you want to save time, other MATLAB users have created functions for reading, parsing, and finding elements of an XML file. Several of these types of functions can be found by doing a search on the MathWorks' File Exchange website.
Attached to this solution is 'xml_example.zip' which contains a simple example to illustrate how to put this all together. In the example, I have created a custom block that uses a MATLAB S-Function to read an XML file containing two initial state values. The block only reads and parses the XML file once at the start of the simulation. The S-Function then outputs, at each time step, the initial states from the XML file. I then display the outputs using two Display blocks.
In order to simplify things, I used the XML Parse-n-Find code provide by user Kesh Ikuma on the File Exchange to read and parse the XML file. This code is available online at:
Please note that The MathWorks does not guarantee or warrant the use or content of these submissions on the File Exchange. Any questions, issues, or complaints should be directed to the contributing author.
In order to run the example, download and unzip the attached file ('xml_example.zip') and then run the model file 'xml_read_example.slx'. The following is a list of the contents of the zipped file and a description of each file:
1) xml_read_example.slx : example model that uses the custom xml read block that I created.
2) xmlfile.xml : the xml file that I read and parse. It contains two simple initial state conditions.
3) xmlReadBlock.m : Level-2 MATLAB S-Function that defines the custom xml read block. Please review this code to see how I set up the block and read the xml file.
4) xmlelem.m, xmlparse.m, xmlfind.m, license.txt : XML Parse-n-Find code and license found on the File Exchange.