xxsimWriteLogToCsv

PURPOSE ^

xxsimWriteLogToCsv - writes logged variables to a comma separated value (csv) file.

SYNOPSIS ^

function [retval] = xxsimWriteLogToCsv(logFileName, logVariables)

DESCRIPTION ^

 xxsimWriteLogToCsv - writes logged variables to a comma separated value (csv) file.

 Syntax: 
   retval = xxsimWriteLogToCsv(logFileName, logVariables)  
 
 Inputs:
   logFileName  = name of csv file e.g 'parametersList.csv'.
   logVariables = list of variables e.g {'time', 'LogVarTest.v1'}.
                  Note: make sure the variable name exists in active model.
                        If not, an error message will be returned.

 Outputs: 
   retval = returns true if all arguments are valid, false otherwise.

 Examples:
    xxsimWriteLogToCsv('Example.csv','time');
        Write the log variable time to Example.csv.
        Note: time should be set as log variable before simulation, by using
        xxsimSetLogVariables.
    xxsimWriteLogToCsv('Example.csv',{'time','Position.x'});
        Write the log variables time and Position.x to Example.csv.
        Note: time and Position.x should be set as log variable before 
        simulation, by using xxsimSetLogVariables.

 See also: xxsimSetLogVariables

 Author: Controllab Products B.V.
 email: info@controllab.nl
 Website: http://www.controllab.nl
 September 2013

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [retval] = xxsimWriteLogToCsv(logFileName, logVariables)
0002 % xxsimWriteLogToCsv - writes logged variables to a comma separated value (csv) file.
0003 %
0004 % Syntax:
0005 %   retval = xxsimWriteLogToCsv(logFileName, logVariables)
0006 %
0007 % Inputs:
0008 %   logFileName  = name of csv file e.g 'parametersList.csv'.
0009 %   logVariables = list of variables e.g {'time', 'LogVarTest.v1'}.
0010 %                  Note: make sure the variable name exists in active model.
0011 %                        If not, an error message will be returned.
0012 %
0013 % Outputs:
0014 %   retval = returns true if all arguments are valid, false otherwise.
0015 %
0016 % Examples:
0017 %    xxsimWriteLogToCsv('Example.csv','time');
0018 %        Write the log variable time to Example.csv.
0019 %        Note: time should be set as log variable before simulation, by using
0020 %        xxsimSetLogVariables.
0021 %    xxsimWriteLogToCsv('Example.csv',{'time','Position.x'});
0022 %        Write the log variables time and Position.x to Example.csv.
0023 %        Note: time and Position.x should be set as log variable before
0024 %        simulation, by using xxsimSetLogVariables.
0025 %
0026 % See also: xxsimSetLogVariables
0027 %
0028 % Author: Controllab Products B.V.
0029 % email: info@controllab.nl
0030 % Website: http://www.controllab.nl
0031 % September 2013
0032 
0033 %------------- BEGIN CODE --------------
0034     %check if the model file exists
0035     if ~exist(logFileName, 'file')
0036         %create an empty file in order to retrieve the absolute file path
0037         fid = fopen(logFileName, 'w');
0038         fclose(fid);
0039     end    
0040     
0041     [status, result, msgid] = fileattrib(logFileName);
0042     if status == 1 
0043     
0044         % use the full resolved name, and we're done
0045         useFilename = result.Name;
0046     else
0047     
0048         % check if the user provided a full name
0049         [directory, name, extension] = fileparts(logFileName);
0050     
0051         % is an extension given?
0052         if isempty(extension) 
0053             extension = '.csv';
0054         end
0055         
0056         % is a directory given?
0057         if isempty(directory)
0058             % use the current directory
0059             directory = pwd;
0060         end
0061         
0062         useFilename = fullfile(directory, cstrcat(name, extension));
0063     end
0064 
0065     retval = xrlinvoke('xxsim.simulator.writeLogToCsv', struct('name', useFilename, 'variables', {{ logVariables, 'array' }} , 'overwrite', true )  );
0066     if ( retval == false )
0067         error('could not write log to csv-file');
0068     end
0069 end
0070 %------------- END OF CODE --------------

Generated on Thu 25-Apr-2024 10:30:24