How to read workspace variables for generated code of simulink model in C code?

4 ビュー (過去 30 日間)
Samer Hanna
Samer Hanna 2017 年 3 月 8 日
編集済み: Spruha 2025 年 6 月 27 日
I am new to the code generation of simulink models. I am generating C code of a Simulink model. The model reads many variables from Matlab workspace. I want to make the generated C Model also read these variables somehow (maybe form .mat file) and load them somehow to a workspace where all blocks can read. What is the best way to do this ?

回答 (1 件)

Spruha
Spruha 2025 年 6 月 20 日
編集済み: Spruha 2025 年 6 月 27 日
Hi Samer,
I understand you are trying to use variables from the MATLAB workspace in your generated C code and make them accessible across all blocks in the Simulink model.
This can be done using the following approaches:
  • Save the variables in a .mat file and use ‘coder.load’ to import them as constants during code generation. This embeds the values directly into the generated code.
  • For scenarios where variables need to be loaded at runtime, use ‘coder.write’ to generate ‘coderdata’ files and access them dynamically using ‘coder.read’.
  • Alternatively, for signal or time-series data, you can use From Workspace or From File blocks to load data into the model.
Please refer to the official documentation for information on ‘coder.load’ and data read/write operations in generated code
Hope this helps!

カテゴリ

Help Center および File ExchangeSimulink Coder についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by