How to declare a global variable in a large Simulink model?

6 ビュー (過去 30 日間)
Charles Wang
Charles Wang 2011 年 11 月 10 日
回答済み: Foad Sojoodi Farimani 2019 年 2 月 20 日
In c-code to declare a global variable is very easy. But I found that it was not so easy in a large Simulink model with several subsystems.
I used a Memory block with a Memory_Read block followed by a GOTO block. Then use FROM blocks at places where this variable was used as input signal (and Memory_Write block to update).
Problems came when this variable was needed (as output) inside an inner level of a subsystem. This variable name would not be recognized there even after adding an input port on the subsystem (unnecessarily) to "feed in" a signal with this name. The only thing I could do was to name an output with this name. When generating code, I found that RTW added a suffix to this name (an operation known as "mangle", meaning that Simulink thinks that it is a diiferent variable so gives it a different name). How to tell Simulink that it is a global variable and not to change its name?
I searched Matlab Help but did not find any advices. My solution was to manually edit the generated code, but was unhappy with the solution. Please help. Thanks!

回答 (4 件)

Kaustubha Govind
Kaustubha Govind 2011 年 11 月 10 日
It appears that what you need is a Data Store Memory block. You can use the Data Store Read and Data Store Write blocks to read/write to it respectively. You can also customize how this data is represented in generated code bu changing the storage class, as described here.
  2 件のコメント
Charles Wang
Charles Wang 2011 年 11 月 12 日
Thank you for the tip. I added a signal object for the Data Store Memory according to Mathworks document, but found that it had no effect on my case.
In my case I need to set the global variable inside a chart within a subsystem. I created an data with the same name in the chart outputing to Simulink so the name was an output port on the chart at the same time. I don't think that Simulink would consider it related to the Data Store Memory even they beared the same name. (I did connect it to Memory Write on the subsystem containing the chart, but inside the chart Simulink added a suffix to this name.)
This is a weakness where C-language is favored to Matlab/Simulink.
Kaustubha Govind
Kaustubha Govind 2011 年 11 月 14 日
You need to connect your chart output to a Data Store Write block (http://www.mathworks.com/help/toolbox/simulink/slref/datastorewrite.html), not a Memory Write block. Could you try if this fixes the issue?

サインインしてコメントする。


Kyon
Kyon 2016 年 4 月 9 日
how can i make the output of a block or a subsystem a global variable to write to blocks and Matlab function function blocks?

Ricardo A. Baila
Ricardo A. Baila 2016 年 4 月 12 日
I don't know if it could be simply this, but did you declare the goto's visibility as global?
  1 件のコメント
Ye Fei
Ye Fei 2018 年 8 月 2 日
Not if in a triggered subsystem

サインインしてコメントする。


Foad Sojoodi Farimani
Foad Sojoodi Farimani 2019 年 2 月 20 日
This is strange because in Scilab-xcos and Scicoslab-scicos one can write a script in the context including some global variables :
Untitled1.png
Annotation 2019-02-20 104116.jpg

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by