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

Structure of .mat file

Structure of .mat file

Hi all,
I failed to find any documentation regarding the .mat file, which is created during the simulation. Could someone point me out, how to tell which data corresponds to which variables? Not only the descirption, but also the name vars are malformed in matlab2008b import, what is the difference between data_1 and data_2? The order of variables seems quite random.

The same (or similar) file is exported when simulating in dymola, but I cannot find any doc to it neither. Just the name and desc werent malformed, but the order in data_2 didnt make any sense too.

Thanks all.

Re: Structure of .mat file

I'll go out soon, but I can give you a quick reply:
http://www.mail-archive.com/openmodelic … 01133.html

Hope it's enough

Re: Structure of .mat file

Hello Jez,
I'm using the following function to import OM-Data into Octave / Matlab

Code:


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: Structure of .mat file

Hello,

there is a python lib to work with the mat-files from OpenModelica and Dymola DyMat: http://www.j-raedler.de/projects/DyMat/

Maybe it is useful for some users.
sgroehl

Re: Structure of .mat file

Hi cschaad
    Thanks for your amazing solution. It really helps.
    Is there an idea to read the optimization _res.mat result? Since the structure between optimization  _res.mat and simulation _res.mat are nearly the same.
    I tried to read optimization  _res.mat file and it comes an error on line “ if (isempty(strfind(name(i,current/smile,'der('))) ”
Really appreciate for your kind help.
Thanks

Re: Structure of .mat file

While, I finally got the solution. Here is the modified code, which can be used for the simulation results file and optimization result file. An extra bug is also fixed.
Note that the absolute address is used for the input "resultFile", which can be acquired by:
%% for simulation
sim_str = ['simulate(' simModleName ', startTime=0.0, stopTime=5.0)']
sim_res = omc.sendExpression(sim_str)
omimport(sim_res.resultFile)

%% or for optimization
opt_str = [' optimize(OptClassTemplate, numberOfIntervals=16, stopTime=1, tolerance=1e-8) ']
opt_res = omc.sendExpression(opt_str) % optimization
omimport(opt_res.resultFile);
-------------------------------------------------------------------------------------------------------

Code:


function omimport(resultFile)
% a siltly change including            load ([modelname,'_res.mat'])   ->  load(resultFile)
% and                                                                  -> for i=1:size(name,1)
% and                                  add                             if name(i,1)~="$" 
%
% 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']);
load(resultFile);
%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:size(name,1)
    name(i,:)=strrep(name(i,:),'[','_');
    name(i,:)=strrep(name(i,:),']','_');
    name(i,:)=strrep(name(i,:),'(','_');
    name(i,:)=strrep(name(i,:),')','_');
    name(i,:)=strrep(name(i,:),'$','D');
end

for i=1:size(name,1)
    if (isempty(strfind(name(i,:),'der(')))
        nonchars=strfind(name(i,:),char(0));
    if dataInfo(2,i)<0
        try
        assignin('base','temp',-data_2(-dataInfo(2,i),:));
        catch
        end
    else
        try
        assignin('base','temp',data_2(dataInfo(2,i),:));
        catch
        end
    end
    try
    evalin('base',(['temp(deltat0)=[];']));
    evalin('base',([num2str(name(i,1:nonchars(1)-1)),'=temp;']));
    catch
    end
    end
end
clear data_1 data_2 Aclass description modelname i dataInfo temp deltat0;
evalin('base',(['clear name data_2 dataInfo nonchars temp']));

Edited by: shawnli89 - Oct-05-19 09:42:34

Re: Structure of .mat file

I have slightly modified the code by adding a couple of lines to extract parameters as well and cleaned up a bit for consistency. Tested with MATLAB R2019b Update 5.

Code:


function omimport( resultFile )
%
% Read OpenModelica Result File into Workspace
%
% SYNTAX: omimport(  resultFile )
% z.B. omimport('package.model')
%
% Feedback/problems: Christian Schaad, ingenieurbuero@christian-schaad.de
%
% BU added a few lines to extract parameters and cleaned up the clear
% statement. Note that the function now clears OpenModelica variables
% BC0_1 BC0_2 BC0_3 $cse1 $STATESET1. If you need them remove their names
% from the clear command on the last line.

load( resultFile ); %#ok<LOAD>

% sort out double times
deltat0 = find( diff( data_2(1,:) ) == 0 ); %#ok<*USENS>
disp( ['Removed same time values: ', ...
    num2str( length( deltat0 ) ), '/', ...
    num2str( length( data_2(1, :) ) )] )

% assignments to base workspace
assignin( 'base', 'deltat0', deltat0 );
name = name.'; %#ok<*NODEF>

% replaces round and square parantheses with underscore and $ with D
for i = 1 : size( name, 1 )
    name(i, :) = strrep( name(i, :), '[', '_' );
    name(i, :) = strrep( name(i, :), ']', '_' );
    name(i, :) = strrep( name(i, :), '(', '_' );
    name(i, :) = strrep( name(i, :), ')', '_' );
    name(i, :) = strrep( name(i, :), '$', 'D' );
end

% loops over all outputs
for i = 1 : size( name, 1 )
   
    if ( dataInfo(1, i ) == 1 ) % extracts parameters
       
        try
            assignin( 'base', 'temp', data_1( dataInfo(2, i), : ) );
            evalin( 'base', ...
                ([num2str( name(i, 1 : nonchars(1) - 1)), '=temp;']) );
        catch
        end
       
    else % extracts variables
       
        % does not extract (time) derivatives of variables
        if ( ~contains( name(i, :), 'der_' ) ) % BU change for consistency
           
            nonchars = strfind( name(i,:), char(0) );
           
            if ( dataInfo(2, i) < 0 ) % if dataInfo negative then changes sign
               
                try
                    assignin( 'base', 'temp', -data_2(-dataInfo(2, i), :) );
                catch
                end
               
            else
               
                try
                    assignin( 'base', 'temp', data_2(dataInfo(2, i), :) );
                catch
                end
               
            end % if ( dataInfo(2, i) < 0 )
           
            try
               
                % removes variable if it is same time
                evalin( 'base', ('temp(deltat0) = [];') );
               
                % assigns variable
                evalin( 'base', ...
                    ([num2str( name(i, 1 : nonchars(1) - 1)), '=temp;']) );
               
            catch
               
            end % try
           
        end     % if ( ~contains( name(i, :), 'der_' ) )
       
    end         % if ( dataInfo(1, i ) == 1 )
   
end             % for i

evalin( 'base',('clear deltat0 BC0_1 BC0_2 BC0_3 Dcse1 DSTATESET1') );

Edited by: bogey - Aug-05-20 01:42:31
There is 1 guest and 0 other users also viewing this topic