How can I skip initialization for a variable in further executions?

I want to skip initialization of my variable in further executions of my function, except for the first time. e.g.
w_hat = w_nom; %initialization
w_hat = w_hat + del_w; %update of my variable
So for the first time I want w_hat to hold the value of w_nom, but in the next step when the function is recalled, I want w_hat to hold the value from its previous update. So how do I skip the initialization in the second and further steps?
Thanks in advance! I hope I was clear with my question.
**Updated: I will implement the function file in SIMULINK and so except for the first time the function is called, the objective is to initiate w_hat with the previous updated w_hat.

1 件のコメント

Raghav
Raghav 2015 年 4 月 8 日
You can make use of persistent variables in this situation. Ex:
persistant w_hat;
if(isempty(w_hat))
w_hat = w_nom; %initialization
else
w_hat = w_hat + del_w; %update of my variable
end
Have a look at 'inmem' command as well. Hope this is what you were looking for.

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

回答 (1 件)

Star Strider
Star Strider 2015 年 3 月 24 日

1 投票

You are not re-initialising it, you are reassigning it with a new value with the update.
I believe that is what you want to do.

8 件のコメント

Jilan Samiuddin
Jilan Samiuddin 2015 年 3 月 24 日
I believe when I said "skip initialization", I meant reassigning it it with a new value with the update. So you are spot on Star.
Star Strider
Star Strider 2015 年 3 月 24 日
Thank you! Sometimes I guess correctly.
Jilan Samiuddin
Jilan Samiuddin 2015 年 3 月 24 日
Now help me out please :)
Star Strider
Star Strider 2015 年 3 月 24 日
I already did. You’re not initializing it later, you’re assigning a new value to it. I get the impression that’s what you want to do.
Guessing here because you haven’t actually described what you want, but if you’re doing it in a loop, see if something like this works:
w_hat(1) = w_nom; %initialization
for k1 = 1:whatever
w_hat(k1+1) = w_hat(k1) + del_w; %update of my variable
... OTHER CODE ...
end
Jilan Samiuddin
Jilan Samiuddin 2015 年 3 月 25 日
what you have suggested would work perfectly fine if I was using it as a script file, but when I am using it as a function file in the simulink, it does not work anymore. Because, everytime the function is being called during the simulation, w_hat will start off with w_nom rather than the updated w_hat.
I am not sure if I could make myself clear. Let me know if it is so. Thanks for your feedback though - really appreciate the time you are giving!
Star Strider
Star Strider 2015 年 3 月 25 日
You didn’t say before that you are using Simulink. I haven’t used Simulink in a while. I can’t help you with Simulink.
I have added ‘Simulink’ to the product tags for your question. Maybe someone who is fluent with Simulink can help you from here.
Jilan Samiuddin
Jilan Samiuddin 2015 年 3 月 25 日
Thanks man. :)
I just hope I get some answer soon because it is pretty much urgent for me.
Star Strider
Star Strider 2015 年 3 月 25 日
My pleasure!

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

カテゴリ

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

製品

質問済み:

2015 年 3 月 24 日

編集済み:

2015 年 5 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by