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 exist('xxsim')
0041         xxsimVerbose = xxsim.verbose;
0042     end
0043 
0044     submodelName = '';
0045     if length(varargin)> 4
0046         error('Too many input arguments.');
0047     end
0048     if length(varargin)< 2
0049         error('Too few input arguments.');
0050     end
0051     
0052     % first argument must hold target information
0053     if ~ischar(varargin{1})
0054     
0055         if isnumeric(varargin{1})
0056             targetIndex = varargin{1};
0057             
0058             % get a list of Matlab-Code targets, to determine the index
0059             targets = xxsimGetMatlabCodeTargets();
0060             
0061             if length(targets) < targetIndex 
0062                 error('Wrong target index given');
0063             end
0064             
0065             % and set the name of this target
0066             targetName = targets(targetIndex).name;
0067 
0068         else
0069             error('target name must be a string');
0070         end
0071     else
0072         targetName = varargin{1};
0073     end
0074 
0075     if length(varargin)>=2
0076         if ~ischar(varargin{2})
0077             error('target directory must be a string');
0078         end
0079         outputDirectory = varargin{2};
0080     end
0081 
0082     if length(varargin)>=3
0083         if ~ischar(varargin{3})
0084             error('submodel name must be a string');
0085         end
0086         submodelName = varargin{3};
0087     else
0088         submodelName = '';
0089     end
0090     
0091     if length(varargin)>=4
0092         if ~ischar(varargin{4})
0093             error('templateDirectory must be a string');
0094         end
0095         templateDirectory = varargin{4};
0096     else
0097         templateDirectory = '';
0098     end
0099 
0100     if( xxsimVerbose )
0101         fprintf('Generating Matlab-Code for : ');
0102     end
0103 
0104     codeGenResult = xrlinvoke('xxsim.simulator.generateMatlabCode', struct('targetName', targetName, 'outputDirectory', outputDirectory, 'submodelName', submodelName, 'templateDirectory', templateDirectory));
0105     if( isfield(codeGenResult, 'errors') && length(codeGenResult.errors) ~= 0 )
0106         result = 0;
0107     else
0108         result = 1;
0109     end
0110     
0111     % check if the code generation itself returned an error
0112     if result ~= 0
0113         if isfield(codeGenResult, 'result')
0114             result = codeGenResult.result;
0115         else
0116             result = false;
0117         end
0118     end
0119     
0120     %optionally return the outputs as the third variable
0121     if isfield(codeGenResult, 'warnings')
0122         warnings = codeGenResult.warnings;
0123     else
0124         warnings = [];
0125     end
0126     
0127     if isfield(codeGenResult, 'errors')
0128         errors = codeGenResult.errors;
0129     else
0130         errors = [];
0131     end
0132     
0133     if( xxsimVerbose )
0134         if( result )
0135             fprintf('Success\n');
0136         else
0137             fprintf('Failure\n');
0138         end
0139 
0140         % print the errors and warnings
0141         for i = 1:length(codeGenResult.errors)
0142             fprintf('\tError: %s\n',codeGenResult.errors{i});
0143         end
0144         for i = 1:length(codeGenResult.warnings)
0145             fprintf('\tWarning: %s\n', codeGenResult.warnings{i});
0146         end
0147         fprintf('\Code generation: %d error(s), %d warning(s)\n', length(codeGenResult.errors), length(codeGenResult.warnings));
0148     end
0149 end
0150 %------------- END CODE --------------

Generated on Sun 10-Dec-2017 19:24:51