2

To save the time, I'm trying to read the .mat file rather than simulate the model again.

I used scipy.io.loadmat but it didn't work well:

res = loadmat('ChatteringControl_result.mat')
res.keys()

['Aclass', 'dataInfo', 'name', 'data_2', 'data_1', 'description']

The keys are not variable names, and I don't know how to get the variable values.

Then I searched for resolutions, and found DyMat, it works well for other variables but cannot get time.

res1 = DyMat.DyMatFile('ChatteringControl_result.mat')
T = res1['T']
t = res1['time']

KeyError: 'time'

So, how can I get all the results in JModelica?(Without open Matlab of course.)Like, a built-in function in JModelica?

BIG THANKS!

CY Ye
  • 23
  • 3
  • I don't know, are these .mat files standard mat files? I had a similar problem opening a mat file generated by OpenModelica, using scipy.loadmat, too, I think. – Christoph Dec 09 '19 at 19:07
  • ah, yeah, from the DyMat description: "regular mat-files, but use a special variable structure to store the data efficiently." that could use a more detailed description - maybe the data is hidden within one of the entries you found? – Christoph Dec 09 '19 at 19:09
  • 1
    @Christoph I think they are called **level-4** mat-files, it seems that many package only support **level-5** mat-files...(e.g., mat4py) – CY Ye Dec 10 '19 at 04:58
  • @Christoph I print out all the keys but just can't find `'time'`.(sad – CY Ye Dec 10 '19 at 05:05
  • 1
    This worked for me: https://github.com/thorade/jupyterNotebooks/blob/master/dymat/CSV_Plot.ipynb – matth Dec 10 '19 at 07:20

2 Answers2

3

https://openmodelica.org/doc/OpenModelicaUsersGuide/latest/technical_details.html#the-matv4-result-file-format describes the format. I think you can also look in a Dymola manual for more details.

As for DyMat, there is no reason to get the time trajectory because you typically lookup what value a variable has at a certain time. The start and stop-times are in the data_1 matrix as far as I remember (or typically get it from the first trajectory in the data_2 matrix). (The data_2 matrix may be interpolated, so the time values stored in it may not reflect the actual steps taken internally by the solvers)

sjoelund.se
  • 3,468
  • 12
  • 15
3

To load the mat file using JModelica you can use this code:

from pyfmi.common.io import ResultDymolaBinary

res = ResultDymolaBinary("MyResult.mat")

var = res.get_variable_data("myVar")

var.t #Time trajectory
var.x #Variable trajectory
Christian Winther
  • 1,113
  • 1
  • 9
  • 17