Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

writing variables to mat file

writing variables to mat file

Hallo,

i want to write some selected variables into a mat file. this can be decomposed in 2 questions:

1) openmodelica (at least 1.6.0) can write results as .mat. is this feature (writing mat files) accessible for users? e.g. writing data in a mat file (not the default result file) from within a simulation or script?
I would have linked against the matlab libraries, wrote some modelica wrappers to the library - but if openmodelica can do it why should i reinvent the wheel?

2) is there a user side access to the 'output event':
what i mean is if i would like to write some variables in a file x times per second, i could explicitely write a sample statement, therein call the corresponding code (modelica or c) writing the data to a file.
on the other hand modelica has the output interval setting. is it possible to use a part of that mechanism - either 'reading' the output interval setting & using it in a sample block or even better register a function to be executed when output is written...


thank you

alex

Re: writing variables to mat file

Hi,

1.  yes. you use simulate(Model, outputFormat="mat");
2.  you could use sample in a when equation in which you have an (external) function call that writes to a file
      when (sample ...) then
         callFunctionWriteResults(x, y, z);
     end when;
     Your function will be called at that specific sample.
     If you only need the results output from the callFunctionWriteResults then you can simulate
     using simulate(Model, outputFormat="empty") which does not generate any result file.

Cheers,
Adrian Pop/

Re: writing variables to mat file

2. thats exactly my plan: setting output=empty and writing only variables in which I'm interested in.
but using sample is the explicit way to do it.
what I wonder is if there is a possibility to get hands on the output interval setting.... otherwise one would need to specify the sample interval manually in the model...


1. yes, I have seen that i can select .mat as output file. what I want is different:
suppress all output  outputFormat="empty".

at sampled times write some specific variables to a .mat file (as discussed above). the question is: do I need to link and call matlabs mat libs or can i use openmodelica onboard code (as you stated om actually can write .mat files)....


alex

Re: writing variables to mat file

Hi,

simulate(Model, startTime=0, stopTime=1, numberOfIntervals=5, outputFormat="mat")
the output interval (the step) is equal to (stopTime-startTime)/numberOfIntervals.

You can get the behavior you want only by changing the sources in trunk/c_runtime/.
Right now we do not have a way to set a callback on the emit function for the variable output.
We should support such functionality as it would be good.

Cheers,
Adrian Pop/

Re: writing variables to mat file

ok,
and how about the mat file interface - where can I find it?
guess there is some c-code which I didn't find...

shouldn't be to much coding in order to wrap this code in order to provide a modelica library allowing to write mat files - or not?

alex

Re: writing variables to mat file

Hi,

trunk/c_runtime/simulation_result_mat.cpp
trunk/c_runtime/simulation_result_mat.h
In the emit() method the output to file is realized.
You can add your filters there.

Note that the generated model code is linked with libc_runtime.a

Cheers,
Adrian Pop/

There are 0 guests and 0 other users also viewing this topic
You are here: