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

OpenModelica-Matlab

OpenModelica-Matlab

hi.. I would like to load a modelica model in Matlab. Does anyone can help me? How can I use this function?




function omimport(modelname) 
%
% Read OpenModelica Result File into Workspace
%
% SYNTAX: omimport(modelname)
% z.B. omimport('package.model')
%
% Feedback/problems: Christian Schaad, ingenieurbuero@christian-schaad.de

load ([modelname,'_res.mat']);
%Sort out double times
deltat0=find(diff(data_2(1,current/smile)==0);
disp(['Removed same time values: ',num2str(length(deltat0)),'/',num2str(length(data_2(1,current/smile))])
assignin('base','data_2',data_2);
assignin('base','dataInfo',dataInfo);
assignin('base','name',name);
assignin('base','deltat0',deltat0);
name=name';
for i=1:length(name)
if (isempty(strfind(name(i,current/smile,'der(')))         
nonchars=strfind(name(i,current/smile,char(0));
if dataInfo(2,i)<0;
assignin('base','temp',-data_2(-dataInfo(2,i),current/smile);
else
assignin('base','temp',data_2(dataInfo(2,i),current/smile);
end
evalin('base',(['temp(deltat0)=[];']));
evalin('base',([num2str(name(i,1:nonchars(1)-1)),'=temp;']));

end
end
clear data_1 data_2 Aclass description modelname i dataInfo temp deltat0;
evalin('base',(['clear name data_2 dataInfo nonchars temp']));

Re: OpenModelica-Matlab

Hi

To use this function, first you have to simulate the model in OpenModelica. If the option "Output Format" is selected with "mat" (by default), in the path "C:\Users\User_Name\AppData\Local\Temp\OpenModelica\OMEdit" it will appear a file with the extension .mat and "modelname_res".

Open a Matlab session and copy this file into the current Matlab directory. Now you can run the function from the command line.

Cheers,
Jorge

Re: OpenModelica-Matlab

do i have to change something in the function?

Re: OpenModelica-Matlab

The function must work, but if you get some errors, make sure your Matlab version is compatible with the function one.

Re: OpenModelica-Matlab

Hi guys,
I have modified the code in order to work smoothly in Matlab R2018b.
There are two main differences:
- the output is not into the workspace, but into a structure
- arrays keep their shape, i.e. force[3] will be an array and will not be split into force_1, force_2, force_3 as before

Here is the attachment:
omimport.m

Attachments:

Re: OpenModelica-Matlab

mat files created by OpenModelica and Dymola have the same structure. They are matlab files, but require some processing to distinguish variables.

I did this in my  own plotting and post-processing program, which is Modelica-aware. In case of interest you can find it here:

https://drive.google.com/open?id=0B7FNP … WtBLVlwMEk

Maybe you are just satisfied from what this program does.
Otherwise, if you really need Matlab, I can share the my cpp routine which reads the mat file. However, you need to do some simple reverse-engineering to understand its details and replicate them in Matlab.

There are 0 guests and 0 other users also viewing this topic