This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

generateFiles

Generate MATLAB files for code generation using coder configurer

Syntax

generateFiles(configurer)
generateFiles(configurer,'OutputPath',outputPath)

Description

example

generateFiles(configurer) generates the MATLAB® files required to generate C/C++ code by using the coder configurer configurer, and saves the generated files in the current folder.

To customize the code generation workflow, use generateFiles and codegen. If you do not need to customize your workflow, use generateCode.

generateFiles generates the following MATLAB files:

  • predict.m, update.m, and initialize.mpredict.m and update.m are the entry-point functions for the predict and update functions of the machine learning model, respectively, and these two functions call initialize.m. You can modify these files according to your code generation workflow. For example, you can modify the predict.m file to include data preprocessing, or you can add these entry-point functions to another code generation project.

  • A MAT-file that includes machine learning model information — generateFiles uses the saveCompactModel function to save machine learning model information in a MAT-file whose file name is stored in the OutputFileName property of a coder configurer. initialize.m loads the saved MAT-file by using the loadCompactModel function.

After you generate these files, generate C/C++ code by using codegen and the prepared codegen argument stored in the CodeGenerationArguments property of a coder configurer.

If the folder already includes all four MATLAB files, then generateFiles does not generate any files.

generateFiles(configurer,'OutputPath',outputPath) generates the MATLAB files in the folder specified by outputPath.

Examples

collapse all

Train a machine learning model and then generate the MATLAB files required to generate C/C++ code for the predict and update functions of the model by using a coder configurer.

Load the ionosphere data set and train a binary support vector machine (SVM) classification model.

load ionosphere
Mdl = fitcsvm(X,Y);

Mdl is a ClassificationSVM object.

Create a coder configurer for the ClassificationSVM object.

configurer = learnerCoderConfigurer(Mdl,X);

configurer is a ClassificationSVMCoderConfigurer object, which is a coder configurer of a ClassificationSVM object.

Use generateFiles to generate the MATLAB files required to generate C/C++ code for the predict and update functions of the model.

generateFiles(configurer)

generateFiles generates predict.m, update.m, initialize.m, and ClassificationSVMModel.mat (a MAT-file that includes machine learning model information).

Display the contents of the predict.m, update.m, and initialize.m files.

type predict.m % Display contents of predict.m
function varargout = predict(X,varargin) %#codegen
% Autogenerated by MATLAB,  13-Apr-2019 03:42:34
[varargout{1:nargout}] = initialize('predict',X,varargin{:});
end
type update.m % Display contents of update.m
function update(varargin) %#codegen
% Autogenerated by MATLAB,  13-Apr-2019 03:42:34
initialize('update',varargin{:});
end
type initialize.m % Display contents of initialize.m
function [varargout] = initialize(command,varargin) %#codegen
% Autogenerated by MATLAB, 13-Apr-2019 03:42:34
coder.inline('always');
persistent model;
if isempty(model)
    model = loadCompactModel('ClassificationSVMModel.mat');
end
switch(command)
    case 'update'
        % Update struct fields: Alpha
        %                       SupportVectors
        %                       SupportVectorLabels
        %                       Scale
        %                       Bias
        %                       Prior
        %                       Cost
        
        model = update(model,varargin{:});
    case 'predict'
        % Predict Inputs: X
        
        X = varargin{1};
        if nargin == 2
            [varargout{1:nargout}] = predict(model,X);
        else
            PVPairs = cell(1,nargin-2);
            for i = 1:nargin-2
                PVPairs{1,i} = varargin{i+1};
            end
            [varargout{1:nargout}] = predict(model,X,PVPairs{:});
        end
end
end

Generate C/C++ code by using codegen and the prepared codegen argument stored in the CodeGenerationArguments property of configurer.

cfArgs = configurer.CodeGenerationArguments;
codegen(cfArgs{:})

Input Arguments

collapse all

Coder configurer of a machine learning model, specified as a coder configurer object created by using learnerCoderConfigurer.

ModelCoder Configurer Object
Support vector machine (SVM) regressionRegressionSVMCoderConfigurer
SVM for one-class and binary classificationClassificationSVMCoderConfigurer
Multiclass model for SVMsClassificationECOCCoderConfigurer

Folder path for the output files of generateFiles, specified as a character vector or string array.

The specified folder path can be an absolute path or a relative path to the current folder path.

  • The path must not contain spaces because they can lead to code generation failures in certain operating system configurations.

  • The path also cannot contain non-7-bit ASCII characters, such as Japanese characters.

If the specified folder does not exist, then generateFiles creates the folder.

generateFiles searches the specified folder for the four MATLAB files: predict.m, update.m, initialize.m, and a MAT-file that includes machine learning model information. If the four files do not exist in the folder, then generateFiles generates the files. Otherwise, generateFiles does not generate any MATLAB files.

Example: 'C:\myfiles'

Data Types: char | string

Alternative Functionality

  • To customize the code generation workflow, use generateFiles and codegen. If you do not need to customize your workflow, use generateCode. In addition to generating the four MATLAB files generated by generateFiles, the generateCode function also generates the C/C++ code.

Introduced in R2018b