How can I export the version number when generating code from a Simulink model?

14 Ansichten (letzte 30 Tage)

I am generating code from my Simulink model and would like to export the model version number to the generated code. I would like the version number either in a header file or in the generated C code. Is this possible?

Akzeptierte Antwort

MathWorks Support Team
MathWorks Support Team am 22 Feb. 2024
Yes it is possible. Depending on your use case there are multiple ways approach this. The first method allows you to include the model version number as a comment while the other methods detail ways to include the Model Version number within the actual code generated by the model.
Method 1: Creating a File Banner to include the model version number as a comment
If you just want to expose the model version number within the comments of the generated code then you may use the 'Code Generation Template' files to specify custom file banners and function banners for your generated code. This method can be done through the 'Configuration Parameters' of your model and you can include information such as the 'Model Version Number', 'Model Name', 'File Type', 'TLC Version', 'Code Generation Settings', etc.
To read more about this method and how to accomplish this, please refer to the following documentation:
 
Method 2: The model version number is used within the logic of the model
If the model version parameter is actively used in your model and will not be optimized out, then you can create a 'Simulink.Parameter' for the value and setup a 'PostSaveFcn' callback to access the model version number. In this case, the parameters storage class was also set to 'Define' so that the model version number was exported to a header file as a macro. To read more about the 'Define' storage class please refer to the following documentation:
This will ensure that a macro containing the model version number will be generated along with the rest of the model. To do something along these lines please refer to these steps:
1) Create the 'Simulink.Parameter' object. In this case it is created in the 'Model Workspace'
2) You may then click 'Configure' for the parameters Storage Class and set it to 'Defined'. You may then click the pencil icon to set the header file name and identifier name of the macro.
3) Then, ensure the Parameter is being used within the model
4) Then, create a 'PostSaveFcn' callback to reassign the value of the parameter every time the model version is changed. Since the Model Version changes on save, this callback will ensure the model version stays updated.
The code for this callback is below:
modelWorkspace = get_param(gcs, 'ModelWorkspace');
param = modelWorkspace.getVariable('ExportedVersion');
param.Value = str2double(get_param(gcs, 'ModelVersion'));
modelWorkspace.assignin('ExportedVersion', param);
5) Now, when the model is built, a 'test.h' file is generated with the desired macro
Please note that this method will only work if the 'ExportedVersion' parameter created is being used within the logic of the model. Otherwise Simulink will optimize it out and thus not generate the desired external header file.
Method 3: Model version is not used within the logic of the model, but the generated code needs to include it
If you would like your model version to be included in your generated code but it is not used in the logic of your model or it is being optimized out of the generated code, you may configure a Test Point. To read about test points please refer to the following documentation:
1) Create the 'Simulink.Parameter' object. In this case it is created in the 'Model Workspace'
2) You may leave the storage class of the 'Simulink.Parameter' as 'auto'
3) Create a Constant Block with the 'Simulink.Parameter' you created in Step 1 and connect it to a Terminator 
4) Then create a 'PostSaveFcn' callback to reassign the value of the parameter every time the Model Version changed. Since the Model Version changes on save this callback will ensure the Model Version stays updated.
(This is the same code as located in Method 2)
5) Configure the signal out of the constant block as a 'Test Point'
6) Generate code and note the model version is exposed in the generated code
Method 4: Model version is not used in the generated code, but still written externally to a header file
If you would like to export the model version without needing the parameter be included in the generated code, you may do so by having Simulink write to a file the value of model version in a callback. To do so please refer to the following:
1) Use the 'PostSaveFcn' callback to write the following MATLAB code
headerFileName = 'model_version.h';
fileId = fopen(headerFileName, 'w');
if fileId == -1
error('Could not open file %s for writing.', headerFileName);
end
fprintf(fileId, '#ifndef MODEL_VERSION_H\n');
fprintf(fileId, '#define MODEL_VERSION_H\n\n');
fprintf(fileId, '#define MODEL_VERSION %s\n\n', modelVersion);
fprintf(fileId, '#endif // MODEL_VERSION_H\n');
fclose(fileId);
This will simply open a file and write out the desired model version to a header file. This will happen every time the model is saved.

Weitere Antworten (0)

Kategorien

Mehr zu Simulink Coder finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by