Results file reformatting for easier use in MATLAB

Let’s make post processing of Dymola results easier

In many circumstances, you may want to handle a results file from a Dymola simulation within MATLAB, for instance undertaking post processing on a simulation or conducting model validation. A quick glance at your Dymola result set shows that it has the .mat file extension, indicating that the result file is outputted in the .mat format. One might assume that this means the result file can be used easily in MATLAB; unfortunately, this isn’t the case.

But with a few Dymola commands and programming principles, you can create a simple tool which enables you to write a MATLAB compatible results file from your Dymola results set.

What’s more, with a few extra lines of code, your tool can give you full control over the result file written for MATLAB, including all variable names you want to use, the result file name and importantly, which variables you write to your MATLAB compatible result file. This is a useful feature if you’re working with a large model with thousands of variables with only a few key results and want to rename those for use in MATLAB.

The function is demonstrated using the coupled clutches example model, although it can be pointed at any Dymola results file desired. The attached pdf document at the bottom of the article contains all the Modelica code to build the function.

Some .mat results files are more equal than others…

Whilst MATLAB will happily open the Dymola result file, the data structure employed in the Dymola results file isn’t a standard MATLAB .mat format. As you can see, MATLAB doesn’t natively read the format of the file as you would expect to see if you opened a MATLAB format .mat file. What in fact you see is the trajectory structure which Dymola employs when writing the results files.

When the coupled clutches result file is opened in MATLAB, the structure displayed in the workspace isn’t consistent with what is displayed in Dymola’s variable browser.

Now, you could work with the file structure you see in MATLAB and manipulate the file into a friendlier format, as trajectory files follow a standard format which is detailed here, but this would require the initial importation of the whole result file into MATLAB. Being a slow and inefficient process, if the result file is large (like say for a vehicle model), it would make more sense to just import the desired variables into MATLAB.

Building the function

As with all Modelica functions, the inputs are required to be defined first. For this application, that will be the path to the results file, the variable names of the trajectory results we wish to write to the MATLAB results, the variables names we want to attribute them to in the MATLAB results file and finally the file name we want the MATLAB results file to have.

The names of the variables as they appear in the results file (their path as shown in the component browser) are required to correctly locate the desired variables in the trajectory file. Any MATLAB compatible syntax of variable name can be used for the new names.

For reasons explained a little further down, you must start your variable list with the Time variable. Dymola adds the Time vector to the trajectory result file automatically, always as the first variable in a results file. Due to the logic we will implement later, the new variable names are required to be in the same position in both the varReNames string and varOrigNames string. So in our example, we want to rename J1.w to Inertia_1_angularVel, therefore they both occupy the second position in their respective vectors.

An annotation has been deployed after the filename variable declaration to enable a file selector dialog box to appear when the edit button is pressed, so the user can navigate to where the results file is located rather than having to input the path manually or be restricted to only pointing the conversion function at trajectory files in the working directory.

A simple annotation to make the loading process intuitive and simple.

Handling any form of data in a script or function like this requires a set of working variables to hold data needed in the processes conducted by the function. The exact purpose of these, shown below, will become apparent as we review each part of the function.

With these variables being of internal use within the function it is good practice to declare them as protected, a keyword which has equivalents in other object oriented programming.

Variables for internal use by the function.

Import the data

The first thing you need to do is to read the Dymola Results file into Dymola. The simplest way to do this is to use the readTrajectory command, which requires knowledge of the size/number of rows within the trajectory file. Thankfully the readTrajectorySize command exists which makes this aspect easy. With the size of the file known, you can now import the desired variables into Dymola from the trajectory file.

Note that the variable varOrigNames is used, which contains the variable names/paths to the variables of interest in the trajectory file.

With the variables of interest imported, we can now derive some key information about the data we are working with which will be of use in the code we construct. Before this, the data is transposed, in order to be written as column matrices into the MATLAB file. As the processing code will be loop based, we will need to know the number of columns we are dealing with. Simply use the Size command with an index of 2 to determine the number of columns.

Transposing of the data is done for ease of handling, with noColumn being used in the processing algorithm.

Write the data to the new MATLAB .mat results file

Eagle eyed readers will have spotted during the variable declaration, the loop counter has a default value of 2, and the variables we specified to import began with the Time vector. Due to the writeMatrix Dymola command only being able to write arrays with 2 dimensions, all the variables written need a second dimension. The time vector is a logical candidate.

The writeMatrix Dymola command, used to create the MATLAB format file, features an “append” option. This will enable us to write a nested if statement loop with a manual counter to rattle through the data writing process, adding data repeatedly. A while-loop is used to denote which column of our imported results set is being written, with the nested if-statement used to differentiate between the writeMatrix command initially creating the file and the same command equipped with an additional argument (true) causing outputs to be appended during every subsequent iteration.

As we have the time vector first in the list of variables to write, the loop counter starting at 2 means we skip to the first of the actual result variables when assigning them to the temporary variable, alongside the Time vector.

A simple if-statement nested in a while-loop can be used to write each column of the data to the MATLAB file. Note the new variable names string variable is used, and each string is called in ascending order of the vector terms, explaining why the positions of the original and new names have to correspond.

Just like all other Modelica functions, true or false (depending on the execution success) is returned at the bottom of the command window in the Simulation tab of Dymola on the function call being completed. Our MATLAB formatted results file can be found in the working directory of Dymola. Loading it into MATLAB, we can see all the variables we have assigned in the workspace as desired, each in a column format with the time vector attached as the first column.

The workspace is a lot less cluttered with only the timeseries variables we desire being present, not to mention the file is much smaller in size!

Depending on your needs, it would be very easy to modify this little function to further process your simulation results before exporting to MATLAB. Some ideas spring to mind, such as specifying a certain time window you want the results for, applying that time window either to all the results you want to export or different time windows for different variables.

You might even want to adapt it so it exports variables grouped together in various blocks rather than individual time series, or in some form of data structure.

A pdf containing all the Modelica code to build this function can be downloaded here.

Written by: Theodor Ensbury – 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


Got a question? Just fill in this form and send it to us and we'll get back to you shortly.


© Copyright 2010-2024 Claytex Services Ltd All Rights Reserved

Log in with your credentials

Forgot your details?