With Dymola storing data in .mat format, it can sometimes be difficult to extract the data for external use. In this blog post I will describe a couple of methods that can be used to export data to .csv files using Dymola scripts.
Dymola can be controlled through other language interfaces and can read the .mat files directly. There is a blog post describing the Python-Dymola Interface; that describes this interface and gives examples of extracting data from results in 2 methods.
Extracting the data
The first step is extract the data from the experiment, this can be done using the readTrajectory function. For that, you first need to read the trajectory size of the result file. That is then given to the readTrajectory function, along with the paths of interest that you want to extract.
For this blog post I will be using the Double pendulum example within the Modelica standard library (Modelica.Mechanics.MultiBody.Examples.Elementary.DoublePendulum). In this case we are extracting the time data, along with the revolute angles.
nSize=DymolaCommands.Trajectories.readTrajectorySize("DoublePendulum.mat"); data=DymolaCommands.Trajectories.readTrajectory("DoublePendulum.mat", {"Time","revolute1.phi","revolute2.phi"}, nSize);
This works because the DoublePendulum.mat exists in the working directory. Otherwise you would need to put the full path of the .mat file.
Printing Results to CSV
The first method of extracting data to a csv is using the print function. This allows you to print each line of the csv individually. This function creates the file and then writes each line. The data needs to be separated into comma separated format, that can be done using a Claytex function stringVectorToDelimitedString.
nSize=DymolaCommands.Trajectories.readTrajectorySize("DoublePendulum.mat"); data=DymolaCommands.Trajectories.readTrajectory("DoublePendulum.mat", {"Time","revolute1.phi","revolute2.phi"}, nSize); Modelica.Utilities.Files.removeFile("pendulumResults.csv"); for i in 1:nSize loop Modelica.Utilities.Streams.print(Claytex.Functions.Strings.stringVectorToDelimitedString(v=String(data[:,i]), delimiter=","), "pendulumResults.csv"); end for;
Note the removeFile function runs before any use of the print function to remove existing file with the same name. This is because without this then the data would be added to that existing file. This loop then runs through each step and prints it to a new line within the pendulumResulsts.csv.
Using the DataFiles library function
As part of every Dymola installation there is the free DataFiles library which includes a function that reads data from a .mat file and then prints it to a CSV. This also includes headings and populates the first column with time data.
DataFiles.convertMATtoCSV("DoublePendulum.mat", {"revolute1.phi","revolute2.phi"}, "pendulumResults.csv");

Written by: David Briant – Senior Project Engineer
Please get in touch if you have any questions or have got a topic in mind that you would like us to write about. You can submit your questions / topics via: Tech Blog Questions / Topic Suggestion.