xxsimGenerateMatlabCode

PURPOSE ^

xxsimGenerateMatlabCode - Generate Matlab-Code using the specified target.

SYNOPSIS ^

function [result, warnings, errors] = xxsimGenerateMatlabCode(varargin)

DESCRIPTION ^

 xxsimGenerateMatlabCode - Generate Matlab-Code using the specified target.

 Syntax: 
   [result, warnings, errors] = xxsimGenerateMatlabCode(targetName, outputDirectory, submodelName, templateDirectory)
   result = xxsimGenerateMatlabCode(targetName, outputDirectory)

 Inputs:
   targetName   = the name of the target to generate Matlab-Code for.
   outputDirectory = the output directory for the generated Matlab-Code.
   submodelName = (optional) the name of the submodel to generate Matlab-Code for.
                   Only necessary for targets that need a submodel selection.
   templateDirectory = (optional) the directory where the code generation template is stored.
                       When omitted, 20-sim used the default 'Matlab-Code Folders' setting instead.

 Outputs:
   result  = returns true if the Matlab-Code generation was successful.
   warnings = warning(s) during Matlab-Code generation, if any.
   errors  = error(s) during Matlab-Code generation, if any.

 Examples:
    result = xxsimGenerateMatlabCode('Matlab','c:/temp/controller','Controller')
        Generate Matlab code for the Controller submodel and put it in C:/temp/controller.
    result = xxsimGenerateMatlabCode('StandAlongSimulink','c:/temp/examples')
        Generate stand-alone Simulink code and put it in C:/temp/examples.
    [result, warnings, errors] = xxsimGenerateMatlabCode('Matlab','c:/temp/controller',Controller)
        Generate Matlab code for the Controller submodel and put it in C:/temp/controller,
        also get the messages (warnings, errors).

 See also: xxsimGetMatlabCodeTarget, sxxsimGenerateCCode

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

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function [result, warnings, errors] = xxsimGenerateMatlabCode(varargin)
0002 % xxsimGenerateMatlabCode - Generate Matlab-Code using the specified target.
0003 %
0004 % Syntax:
0005 %   [result, warnings, errors] = xxsimGenerateMatlabCode(targetName, outputDirectory, submodelName, templateDirectory)
0006 %   result = xxsimGenerateMatlabCode(targetName, outputDirectory)
0007 %
0008 % Inputs:
0009 %   targetName   = the name of the target to generate Matlab-Code for.
0010 %   outputDirectory = the output directory for the generated Matlab-Code.
0011 %   submodelName = (optional) the name of the submodel to generate Matlab-Code for.
0012 %                   Only necessary for targets that need a submodel selection.
0013 %   templateDirectory = (optional) the directory where the code generation template is stored.
0014 %                       When omitted, 20-sim used the default 'Matlab-Code Folders' setting instead.
0015 %
0016 % Outputs:
0017 %   result  = returns true if the Matlab-Code generation was successful.
0018 %   warnings = warning(s) during Matlab-Code generation, if any.
0019 %   errors  = error(s) during Matlab-Code generation, if any.
0020 %
0021 % Examples:
0022 %    result = xxsimGenerateMatlabCode('Matlab','c:/temp/controller','Controller')
0023 %        Generate Matlab code for the Controller submodel and put it in C:/temp/controller.
0024 %    result = xxsimGenerateMatlabCode('StandAlongSimulink','c:/temp/examples')
0025 %        Generate stand-alone Simulink code and put it in C:/temp/examples.
0026 %    [result, warnings, errors] = xxsimGenerateMatlabCode('Matlab','c:/temp/controller',Controller)
0027 %        Generate Matlab code for the Controller submodel and put it in C:/temp/controller,
0028 %        also get the messages (warnings, errors).
0029 %
0030 % See also: xxsimGetMatlabCodeTarget, sxxsimGenerateCCode
0031 %
0032 % Author: Controllab Products B.V.
0033 % email: info@controllab.nl
0034 % Website: http://www.controllab.nl
0035 % October 2014
0036 
0037 %------------- BEGIN CODE --------------
0038     global xxsim;
0039     xxsimVerbose = 0;
0040     if ~isempty(xxsim)
0041         xxsimVerbose = xxsim.verbose;
0042     end
0043 
0044     if length(varargin)> 4
0045         error('Too many input arguments.');
0046     end
0047     if length(varargin)< 2
0048         error('Too few input arguments.');
0049     end
0050     
0051     % first argument must hold target information
0052     if ~ischar(varargin{1})
0053     
0054         if isnumeric(varargin{1})
0055             targetIndex = varargin{1};
0056             
0057             % get a list of Matlab-Code targets, to determine the index
0058             targets = xxsimGetMatlabCodeTargets();
0059             
0060             if length(targets) < targetIndex 
0061                 error('Wrong target index given');
0062             end
0063             
0064             % and set the name of this target
0065             targetName = targets(targetIndex).name;
0066 
0067         else
0068             error('target name must be a string');
0069         end
0070     else
0071         targetName = varargin{1};
0072     end
0073 
0074     if length(varargin)>=2
0075         if ~ischar(varargin{2})
0076             error('target directory must be a string');
0077         end
0078         outputDirectory = varargin{2};
0079     end
0080 
0081     if length(varargin)>=3
0082         if ~ischar(varargin{3})
0083             error('submodel name must be a string');
0084         end
0085         submodelName = varargin{3};
0086     else
0087         submodelName = '';
0088     end
0089     
0090     if length(varargin)>=4
0091         if ~ischar(varargin{4})
0092             error('templateDirectory must be a string');
0093         end
0094         templateDirectory = varargin{4};
0095     else
0096         templateDirectory = '';
0097     end
0098 
0099     if( xxsimVerbose )
0100         fprintf('Generating Matlab-Code for : ');
0101     end
0102 
0103     codeGenResult = xrlinvoke('xxsim.simulator.generateMatlabCode', struct('targetName', targetName, 'outputDirectory', outputDirectory, 'submodelName', submodelName, 'templateDirectory', templateDirectory));
0104     if( isfield(codeGenResult, 'errors') && length(codeGenResult.errors) ~= 0 )
0105         result = 0;
0106     else
0107         result = 1;
0108     end
0109     
0110     % check if the code generation itself returned an error
0111     if result ~= 0
0112         if isfield(codeGenResult, 'result')
0113             result = codeGenResult.result;
0114         else
0115             result = false;
0116         end
0117     end
0118     
0119     %optionally return the outputs as the third variable
0120     if isfield(codeGenResult, 'warnings')
0121         warnings = codeGenResult.warnings;
0122     else
0123         warnings = [];
0124     end
0125     
0126     if isfield(codeGenResult, 'errors')
0127         errors = codeGenResult.errors;
0128     else
0129         errors = [];
0130     end
0131     
0132     if( xxsimVerbose )
0133         if( result )
0134             fprintf('Success\n');
0135         else
0136             fprintf('Failure\n');
0137         end
0138 
0139         % print the errors and warnings
0140         for i = 1:length(codeGenResult.errors)
0141             fprintf('\tError: %s\n',codeGenResult.errors{i});
0142         end
0143         for i = 1:length(codeGenResult.warnings)
0144             fprintf('\tWarning: %s\n', codeGenResult.warnings{i});
0145         end
0146         fprintf('\Code generation: %d error(s), %d warning(s)\n', length(codeGenResult.errors), length(codeGenResult.warnings));
0147     end
0148 end
0149 %------------- END CODE --------------

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