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

Enhanced Script to import results file into Octave

Enhanced Script to import results file into Octave

I am including the Octave script to import Modelica results file (from .mat format) which was originally developed by Christian Schaad, ingenieurbuero@christian-schaad.de
Hope this helps others.


Code:


function omimport(modelname) 
%
% Read OpenModelica Result File into Workspace
%
%
% Feedback/problems: Christian Schaad, ingenieurbuero@christian-schaad.de
% Modified by R. Saripalli,
%            to handle
%         Array variables of form X[i] with  (renamed to X_i) 
%            and to discard trailing non-ascii chars from variable names.
%  Tested on Octave 
%  4th Dec. 2013


load ([modelname,'_res.mat']);

%Sort out double times
times = data_2(1,:) ;
deltat0=find(diff(times)==0); %Vector of indecides with duplicate time stamp
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';
disp(['Number of Variables:', num2str(length(name))]);
for i=1:length(name)
        tmp =  strfind( name(i,:),  'der('  );
    if (isempty(tmp))    % when der is absent

                [S,iendchar]=(regexp(name(i,:),"^[-_.a-zA-Z0-9 [\\]]*"));

          %dataInfo second row holds row index of data_2
                  %  which holds variable i data.
          %  its sign is assigned to data values. (why all this fuss??)
          %  and this data is assigned to varVals.
        if dataInfo(2,i) <0  %dataInfo second row holds row index of data_2
              assignin('base','varVals',-data_2(-dataInfo(2,i),:));
            else
               assignin( 'base', 'varVals', data_2(dataInfo(2,i),:) );
                endif 
           
        evalin( 'base', (['varVals(deltat0)=[];']) )  %We ignore the data if dup. time stamp

        varName = num2str(name(i,1:iendchar));      % Grab the variable name
        varName = strrep(varName, "[","_");
                varName = strrep(varName, "]","");  %Handle variables of array type

        evalin( 'base', ([varName,'=varVals;']) );
    endif
endfor  % end of loop over all variables

clear data_1 data_2 Aclass description modelname i dataInfo varVals deltat0 varName;
evalin('base',(['clear name data_2 dataInfo iendchar varVals']));

endfunction

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