- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » OpenModelica-Matlab
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,)==0);
disp(['Removed same time values: ',num2str(length(deltat0)),'/',num2str(length(data_2(1,))])
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,,'der(')))
nonchars=strfind(name(i,,char(0));
if dataInfo(2,i)<0;
assignin('base','temp',-data_2(-dataInfo(2,i),);
else
assignin('base','temp',data_2(dataInfo(2,i),);
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
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
- DarioMangoni
- 45 Posts
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.
- ceraolo
- 147 Posts
- Index
- » Usage and Applications
- » OpenModelica Usage and Applications
- » OpenModelica-Matlab