Effacer les filtres
Effacer les filtres

Simulink code generation from command line

23 vues (au cours des 30 derniers jours)
Hass
Hass le 9 Juin 2023
Commenté : Hass le 13 Juin 2023
Is there a way to run the C code generation command (of Simulink model) from matlab script instead of clicking on the "Generate Code" button?

Réponse acceptée

Nandini
Nandini le 12 Juin 2023
This can be achieved using the rtwbuild function or the slbuild function, depending on your MATLAB version.
In both examples, replace 'your_model_name' with the actual name of your Simulink model. The code generation command will generate the C code based on the model and its configuration.
Make sure to have the necessary C code generation settings properly configured in your Simulink model, such as the target language, toolchain selection, and other relevant options. These settings can be adjusted through the Simulink model interface or programmatically using the appropriate API functions.
Here's an example of how you can use the rtwbuild function:
% Specify the model name
modelName = 'your_model_name';
% Generate code using rtwbuild
rtwbuild(modelName);
Here's an example using the slbuild function (available in MATLAB R2019a and later):
% Specify the model name
modelName = 'your_model_name';
% Create a configuration object for code generation
cfg = coder.config('lib');
% Set any additional code generation options if required
% cfg.CustomSource = 'your_custom_source.c';
% Generate code using slbuild
slbuild(modelName, cfg);
  4 commentaires
Hass
Hass le 13 Juin 2023
Modifié(e) : Hass le 13 Juin 2023
Hi Nandini, I tried using the ert_shrlib config but I got the error below!
Any clue to why this is?
Thanks
>>run_code_gen
Unrecognized configuration argument
Use help codegen for more information on using this command.
Error using codegen
Error in run_code_gen (line 10)
codegen(model_name,'-config', 'ert_shrlib', '-o', targetDir);
The script is below
run_code_gen.m
model_name = 'stage';
open_system(model_name);
%cs=config;
targetDir = 'stage_ert_rtw';
%cfg = coder.config('lib');
codegen(model_name,'-config', 'ert_shrlib', '-o', targetDir);
%codegen(model_name,'-config', cfg, '-o', targetDir); %did not work
%codegen -config cfg 'stage'
%rtwbuild(model_name);
Hass
Hass le 13 Juin 2023
This worked for me
myConfigObj = getActiveConfigSet(model_name);
switchTarget(myConfigObj,'ert.tlc',[]);
rtwbuild(model_name);

Connectez-vous pour commenter.

Plus de réponses (1)

Manas
Manas le 12 Juin 2023
Hello Hass,
You can use the slbuild function to generate the C code as follows:-
modelName = 'your_model'; % Replace 'your_model' with the name of your Simulink model
% Build the model and generate code
slbuild(modelName);
For More info you can refer to this documentation:

Catégories

En savoir plus sur Deployment, Integration, and Supported Hardware dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by