Main Content

generateFiles

Generate MATLAB files for code generation using coder configurer

Description

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 (MATLAB Coder). 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 saveLearnerForCoder 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 loadLearnerForCoder function.

After you generate these files, generate C/C++ code by using codegen (MATLAB Coder) 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.

example

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.

load ionosphere

Train a binary support vector machine (SVM) classification model, using a Gaussian kernel function with an automatic kernel scale.

Mdl = fitcsvm(X,Y, ...
    'KernelFunction','gaussian','KernelScale','auto');

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, 05-Sep-2024 19:45:21
[varargout{1:nargout}] = initialize('predict',X,varargin{:});
end
type update.m % Display contents of update.m
function update(varargin) %#codegen
% Autogenerated by MATLAB, 05-Sep-2024 19:45:21
initialize('update',varargin{:});
end
type initialize.m % Display contents of initialize.m
function [varargout] = initialize(command,varargin) %#codegen
% Autogenerated by MATLAB, 05-Sep-2024 19:45:21
coder.inline('always')
persistent model
if isempty(model)
    model = loadLearnerForCoder('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 (MATLAB Coder) and the prepared codegen argument stored in the CodeGenerationArguments property of configurer.

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

Input Arguments

collapse all

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

ModelCoder Configurer Object
Binary decision tree for multiclass classificationClassificationTreeCoderConfigurer
SVM for one-class and binary classificationClassificationSVMCoderConfigurer
Linear model for binary classificationClassificationLinearCoderConfigurer
Multiclass model for SVMs and linear modelsClassificationECOCCoderConfigurer
Binary decision tree for regressionRegressionTreeCoderConfigurer
Support vector machine (SVM) regressionRegressionSVMCoderConfigurer
Linear regressionRegressionLinearCoderConfigurer

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 (MATLAB Coder). 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.

Version History

Introduced in R2018b