Why is my variable not staying updated in Simulink

Hello,
I am using Simulink and have a variable set to zero in an enabled subsystem. I send the signal out of the subsystem and am trying to update it as x = x + 1 where x is the signal. After simulink goes through all the blocks once and gets back to where x = x + 1 the x is reset to 0. I have a breakpoint in the enabled subsystem where I initialized it and it never goes back there. Any ideas would be appreciated. Thank you!

2 件のコメント

Kaustubha Govind
Kaustubha Govind 2013 年 6 月 18 日
Could you please explain what block(s) you use to implement the x=x+1 part?
Nicholas
Nicholas 2013 年 6 月 18 日
編集済み: Nicholas 2013 年 6 月 18 日
I am using MatLAB Function blocks. For a simplified example I am using a Matlab function block in the enabled subsystem that defines x = 0; Then I output the signal to the input of another MAtLAB function block outside of the subsystem where x = x + 1; I have a pulse generator for the enable of the subsystem

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

 採用された回答

Nicholas
Nicholas 2013 年 6 月 19 日

0 投票

Actually, I think I figured out what I needed to do. So I declared x as global and found the data store memory block. After I found the block I found http://www.mathworks.com/help/simulink/ug/using-global-data-with-the-matlab-function-block.html#bsdud7d-1 Thank you very much for your time and interest in helping me.

その他の回答 (1 件)

Erwin Torreao Dassen
Erwin Torreao Dassen 2013 年 6 月 18 日
編集済み: Erwin Torreao Dassen 2013 年 6 月 18 日

0 投票

Is this a custom matlab function block? In that case try declaring the variable persistent. (UPDATED)

3 件のコメント

Nicholas
Nicholas 2013 年 6 月 18 日
It is using MatLAB Function blocks. I'm new to MAtlab and am unfamiliar with persistent. I tried it in the subsystem and got an error that outputs can't be persistent. I tried it in the other function block and it says persistent must precede all use of a variable.
Erwin Torreao Dassen
Erwin Torreao Dassen 2013 年 6 月 19 日
I'm not sure I understand what you trying to achieve. My guess of what is happening is that your matlab-function block that defines x = 0 is being used as input outside the subsystem. If that is the case the first block outside this subsytem will always receive 0 as input signal and it will never output more than 1... In fact, your matlab-function block is just a "constant" block.
What is the behavior you want to obtain?
Regards, Erwin
Nicholas
Nicholas 2013 年 6 月 19 日
Thank you for your response. Yes my output is being used outside the subsystem as an input. So the overall goal is that I have a model where I want to measure a signal that goes to my MatLAB function block. Whenever the signal is zero and the simulation reaches that function block I want x to add one and be used as a counter. After the signal is zero for a certain amount of times through and the counter gets so high I want the output of my function block to change. I guess basically I'm not sure how to initialize a variable in simulink. Everytime Simulink's simulation reaches the end of all my blocks it wraps around and starts over again. So when I initialize the signal earlier on the simulation resets the value to 0 again. I thought I'd fixed the problem with the enabled subsystem block but apparently not. Thank you again.

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

カテゴリ

ヘルプ センター および File Exchange で Subsystems についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by