- Using Data Store Memory blocks: To declare such variables explicitly as global, you can create Data Store Memory blocks for them in your Simulink model. These variables will then appear in the code as global, but this requires integrating the Data Store Memory blocks within your model explicitly.
- Simulink.Signal Objects: You can define these variables as "Simulink.Signal" objects in the base workspace or the data dictionary and mark them as "ExportedGlobal". However, if these signals are not referenced in the model, they may still not appear in the generated code. This approach is commonly used in conjunction with MATLAB Function Blocks for sharing variables across different parts of the model.
Generate Code for unused Variables in Data Dictionary
23 ビュー (過去 30 日間)
古いコメントを表示
is it possible to declare some variables as global inside a datadictionary and have these variables show up in a declaration file even if they are unused in the model?
0 件のコメント
採用された回答
Abhas
2024 年 12 月 2 日
In MATLAB, it is not directly possible to declare global variables in a data dictionary and have them appear in a declaration file if they are unused in the model. MATLAB's code generation processes exclude unused variables to optimize the generated code. However, there are workarounds:
You may refer the below MathWorks documentation links to know more about the same:
0 件のコメント
その他の回答 (2 件)
Mark McBroom
2024 年 11 月 30 日
移動済み: Walter Roberson
2024 年 11 月 30 日
AFAIK this is not possible. One simple workaround would be to create global data stores in the model for each of these variables.
0 件のコメント
Sivsankar
2025 年 1 月 24 日 9:17
You might consider creating a MATLAB struct with all the parameters and adding that struct to the SLDD file. This approach ensures that even if only one of the variables is used by the model, all the variable values will be exported to the generated code. You can refer to the following MATLAB answer for a similar query:
Here is a sample snippet illustrating how you can implement this workaround:
%create MATLAB struct
myParams = struct(...
'param1',4,...
'param2',5,...
'param3',6);
%create bus object for the struct
Simulink.Bus.createObject(myParams)
myParamsType = slBus1;
%create Simulink parameter of the struct
myParams = Simulink.Parameter(myParams);
myParams.DataType = 'Bus: myParamsType';
%add the Simulink parameter created to the sldd file
myDictionaryObj = Simulink.data.dictionary.open('filename.sldd');
dDataSectObj = getSection(myDictionaryObj,'Design Data');
addEntry(dDataSectObj,'Params',myParams)
Now, whenever you reference at least one parameter from this MATLAB struct, the generated code will include all the variables from this struct.
Thanks.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Deployment, Integration, and Supported Hardware についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!