Pre-processing .mat files

This post looks at automating pre-processing of .mat files within the Modelica model.

Differing formats

Occasionally the data you have is not in the format required by the Modelica model. So, the data will have to be pre-processed. This can be done by pre-processing all the data. Alternatively this pre-processing can be done in the Modelica model. This post looks at performing this pre-processing in the Modelica model.

Simple Roadfile Example

In this simple example, the old road format is stored as a .mat file. The format is a matrix with columns [s, x, y, z] where s is the distance along the road, x, y and z are the positions in world coordinates. The new road format includes banking, so is [s, x, y, z, banking]. To use the old road file will require a banking column to be added to the table data. In this simple example, the banking column is set to zero using the function in Figure 1.

Figure 1.  Function that reformats the old road data by appending a column of zeros for the banking
Figure 1. Function that reformats the old road data by appending a column of zeros for the banking (in the case that this is actually 0)

In Figure 1, the Modelica Standard Library readMatrixSize, readRealMatrix and writeRealMatrix functions are used as when data in old format file is changed, these changes will be detected and a new output file generated. If the builtin Dymola readMatrixSize, readMatrix and writeMatrix functions are used, then when changes to the old format file are performed, the model has to be re-translated for these changes to be detected (a workaround is to set the flag Advanced.AllowStringParameters=true).

The reFormatRoadSimple function in Figure 1 can be used in a model as in Figure 2 to perform this pre-processing in the model.

Figure 2. Simple road model example that reformats the old road file
Figure 2. Simple road model example that reformats the old road file

In Figure 2 straightRoad.mat is the old format file and _straightRoad.mat is created by the reFormatSimple function. This _straightRoad.mat file is used by a CombiTimeTable component.

This simple road example works; however, it has the disadvantage that on every simulation the data is pre-processed and so _straightRoad.mat is unnecessarily recreated with every simulation.

A more efficient Roadfile Example

It is possible to determine if the old file format has been modified by comparing the modified file time with the previous modified time as in Figure 3.

Figure 3.  Reformat old format file by appending a column of zeros only when the old format file is modified or if the output file does not exist
Figure 3. Reformat old format file by appending a column of zeros only when the old format file is modified or if the output file does not exist

The method in Figure 3 is that when the data is pre-processed the first time, then the modified file time is set to be equal to the modified file time of the old format file. When the file is pre-processed again, the modified file times of the old format file and the new format file are compared and if they are the same then the data is not pre-processed again. The getModifiedTime() function returns the time as a string in yymmddhhmmss format. The setModifiedTimeToFile() function sets the modified file time of the output file to be the same as the old format file.

Note that if you overwrite the oldFormatFile file with another file with the same modification time and name this will not be loaded autom.

Conclusion

This post provides a method for pre-processing .mat files and looks at some modifications to improve the efficiency of pre-processing. The code used in this post is available on request, here.

Written by: Garron Fish – Chief 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

CONTACT US

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

Sending

© Copyright 2010-2021 Claytex Services Ltd All Rights Reserved

Log in with your credentials

Forgot your details?