How can I export a Simulink model that uses the Predict block for use in older MATLAB releases?

5 views (last 30 days)
Given is Simulink model that uses the RegressionNeuralNetwork Predict block. This Simulink block receives as input a trained 'RegressionNeuralNetwork' object, it can be used to perform predictions in Simulink and it was introduced in MATLAB R2021b, see the following link,
It is sought to export this model in a previous Simulink release, but since the latter block was introduced in MATLAB R2021b, it is not possible to simply export the model Moreover, if I try to export an S-Function from this block in Simulink R2021b and use it in an earlier Simulink release I receive the following error message,
Warning:Warning: Variable 'buildOpts' originally saved as a coder.make.BuildOpts cannot be instantiated as an object and will be read in as a uint32.
Error:Error: Error detected: Dot indexing is not supported for variables of this type.
How can I export a Simulink model that uses the Predict block for use in older MATLAB releases?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 2 Dec 2021
Edited: MathWorks Support Team on 2 Dec 2021
Indeed it is not supported to simply create an S-Function from a block in a later Simulink release and use it in an earlier Simulink release due to incompatibility.
 
One way to achieve this workflow is indeed to use S-Functions, but have the corresponding S-Function created and compiled directly in the earlier Simulink release using the code generated in Simulink R2021b. The workflow that will be described herein is very similar to the workflow described in the following MATLAB Answers article,
where in the latter article it is described how to generate code from a Deep Neural Network in MATLAB and have this code embedded into an S-Function. For this workflow the code is generated from the Simulink model using Simulink Coder.
The workflow in this case reads as follows:
 
1.) Please generate code using Simulink Coder for the Simulink model that contains the Predict block in Simulink R2021b. The generated code will consists of such files as myModel.c and myModel.h that contain the model functions, which in turn will contain in this case a step function that calls the algorithm related to the Predict block. The function signature in this case may look as follows,
void myModel_step(void)
where myModel is the name of the corresponding Simulink model. Please note that this function can not be used as entry point for the S-Function block, since it receives no inputs and returns no outputs as it is operating on global variables of the generated code.
2.) Please create a new function either under the same model C-files or under a custom c-file that you add to your project which should have the following signature,
 
/* my_entry_point_function */ double mypredict(double u[n])
where 'n' is an integer indicating the number of inputs that this function should accept and which depends on the number of the inputs that the 'RegressionNeuralNetwork' object is supposed to receive.
Please copy the implementation of function 'myModel_step' into 'mypredict', but this time replace the global variables with corresponding inputs and outputs to the custom function 'mypredict'. For instance, if the input to the Predict block is described by global variable 'model_U.In1', then simply replace the instance of this global variable with the input to the network with 'u', where 'u' is the input variable to custom function 'mypredict'. The same holds for the output variable. If the output variable is called 'model_Y.Out1', then please replace this variable with a local variable that should be returned from function custom function 'mypredict'. This step is herein very important as forgetting to replace some instances of global variable 'model_U.In1' with the input 'u' of the custom function might lead in having parts of the machine learning algorithm, such as the 
standardization
, not applied to your actual input but to the global variable. Please also note to return the local variable that you used to replace global variable 'model_Y.Out1'.
Please provide the declaration of the newly added custom function 'mypredict' in file myModel.h and the definition of this function in myModel.c, so that the generated code can compile without warnings.
3.) After this step has been successfully completed, please test that the code compiles using the bat-file in the generated files from Simulink when using the Simulink Coder to compile the project. Then, please copy this modified code to another location and start the earlier MATLAB release where the model is supposed to be used. The next step is to generate an S-Function using the Legacy Code tool in that earlier MATLAB release. To do so, please type the following commands in MATLAB,
def = legacy_code('initialize'); def.SourceFiles = {'myModel.c', 'myModel.c', 'myModel_data.c'}; def.HeaderFiles = {'multiword_types.h', 'myModel.h', 'myModel_private.h', 'myModel_types.h', 'rtwtypes.h'}; def.OutputFcnSpec = 'double y1 = mypredict(double u1[n])'; def.SFunctionName = 'myModel_SFcn';
where field 'SourceFiles' defines the necessary C-files for the compilation of the S-Function, 'HeaderFiles' the corresponding header h-files, 'SFunctionName' the name of the generated S-Function and finally 'OutputFcnSpec' the specification of the function signature that will be used by the generated S-Function. Please note that 'n' should be a fixed integer depending on the input that this S-Function block should receive and which in this case will depend on the number of inputs to the 'RegressionNeuralNetwork' object.
4.) Lastly, please compile this S-Function and generate a test Simulink model that contains the corresponding S-Function block for this function using the following MATLAB commands,
 
legacy_code('sfcn_cmex_generate', def); legacy_code('compile', def); legacy_code('slblock_generate', def);
For more information about how to use the Legacy Code tool, please visit the following documentation page,

More Answers (0)

Tags

No tags entered yet.

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!