Exporting results to a .mat file to use them as inputs in another model

November 23rd, 2016 by Namkyeong Kim

Written by: Maura Gallarotti – Project Engineer

In this blog post, I will show how to save a result in a .mat file and how to use the .mat file in another model.

As an example, I am going to use the model  Modelica.Fluid.Examples.DrumBoiler.DrumBoiler.

Select the model, go to the simulation tab and simulate it.


Figure 1: Drum boiler model in the package Browser


Figure 2: Drum boiler model (See Astroem, Bell: Drum-boiler dynamics, Automatica 36, 2000, pp.363-378).

Plot the variable SteamValve.port_a.m_flow, which is the flow flowing through the valve StemValve. In the Variable browser, right click on the name of the model that has just been simulated (in this case DrumBoiler) Export result as Choose a name and save as type: Result files -Only plotted (*.mat).


Figure 3: Variable Browser in the Simulation tab


Figure 4: Saving the plotted result as a .mat file

You can also visualize the table directly in Dymola, using a Table Window in place of a Plot Window. Doing so, you will be able to check the stepsize of the table: in this case the stepsize is 10.8 s, being the simulation time 5400 s and the output interval the default value of 500 s. Of course, if you would like to have a different output interval, you can change it in the simulation setup before running the simulation.


Figure 5: How to create a table window


Figure 6: Variable SteamValve.port_a.m_flow in table format.

Create a new model and drag and drop the table Modelica.Blocks.Sources.CombiTimeTable.

Set tableOnFile to truetableName to “data” and write in filename the path where you have saved the m_flowSteam.mat. Simulate the model setting the same StopTime of the DrumBoiler model (in this case it is 5400 s) and plot combiTimeTable.y[1]. Check that the result is the same as SteamValve.port_a.m_flow in the model DrumBoiler.

In case you simulated a model and you forgot to save in a .mat file a specific variable, you can use directly the .mat file that is automatically generated in the working directory with the simulation results.

For doing that, use a combiTimeTable, set tableOnFile to false and write in table:

transpose(readTrajectory(“fileName.mat”, {“Time”,”variablePath1″,”variablePath2”}, readTrajectorySize(“fileName.mat”)))

In this specific case, we are going to write:

transpose(readTrajectory(“DrumBoiler.mat”, {“Time”,”SteamValve.port_a.m_flow”}, readTrajectorySize(“DrumBoiler.mat”)))

Another option is to open the .mat file that contains the simulation results and save the .mat of the specific output you are interested in. To open the results file go to the simulation tab, click on Plot   Open Results and select the .mat file.


Figure 7: How the open the .mat file with the simulation results

If the variable to be exported is not too large, one can also put it directly in a table in Dymola.

For doing so, plot the variable you are interested in (in this case SteamValve.port_a.m_flow). Right click on the legend and select copy.


Figure 8: How to copy the simulation results in a table – step 1

 Then, open the model in which you want to use this data, drag and drop a CombiTimeTable and paste the matrix as explained in figures 9 and 10.


Figure 9: How to copy the simulation results in a table – step 2


Figure 10: How to copy the simulation results in a table – step 3

If in the model there are events, Dymola will store two results for the same timestep. To avoid that, deselect Store variables at events in the Output tab on the Simulation Setup (as shown in figure 11).


Figure 11: How to untick Store variables at events in the Simulation setup

Let’s do a simple example to better understand this last point. Create a new model and drag and drop a source Modelica.Blocks.Sources.Step. Leave the height to 1, the offset to 0 and set the startTime to 0.5 s. Run the simulation, then plot in a table window the variable step.y. Check the column at 0.5 s: you will see that it contains two values: 0 and 1.


Figure 12: Output table in case of events

Now, untick Store variables at events  in the Simulation Setup and rerun the model. You will see now only one value at 0.5 s:


Figure 13: Output table in case of events unticking Store variables at events

Thanks to this simple trick, complex model outputs can be recorded and used as inputs of other models, saving simulation time.


Written by: Maura Gallarotti – Project Engineer