This post looks at automating pre-processing of .mat files within the Modelica model.
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.
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.
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.
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.
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