Set initial value in Matlab Function block

How can I set the initial value for UP? UPI is my initial value input to the block. How can I make sure UP will accepts thhs value only once (start of the simulation)? Thanks
funciton UP=[UPI]
*UP=UPI;
%#close safety_relieve_valve
SRV=0;
if (UP>UL)
Val=0;
UP=RD;
else
Val=1;
UP=RU;
end
if (UP<LL) & (Val==0)
UP=RD+0.1;
disp(UP);
Val=1;
elseif (UP>=LL) & (Val==1)
UP=UP+0.1;
end*

5 件のコメント

William
William 2014 年 4 月 17 日
The issue of the if statement is resolved. Is there any block to extract a single element from the vector (entered from "From Workspace" block) to set as the input to the ML Fctn block?
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 17 日
What do you mean? the same single element each iteration, or the first, then the second, and so on ?
William
William 2014 年 4 月 17 日
I am required to enter input interactively so I used the "From Workspace" block. This block can only input with vector instead of scalar value. I would like to extract a single element from this vector then.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 17 日
have you tried Ports and Data Manager?
William
William 2014 年 4 月 17 日
The input is set as Inherit: same as Simulink. I kept getting error "Expecting scalr...." I changed the type to "double" but no avail.

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

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 17 日
編集済み: Azzi Abdelmalek 2014 年 4 月 17 日

1 投票

You can use persistent declaration
function UP=fcn(UPI)
persistent UPI1
if isempty(UPI1)
UP=UPI
else
UP=UPI1
end
%your code
UPI1=UP;

11 件のコメント

William
William 2014 年 4 月 17 日
What is UP1 & UPI1?
Thanks.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 17 日
UP1 does not exist (The answer was edited) UPI1 is an intermediate variable
William
William 2014 年 4 月 17 日
Referring to my code above, my if (UP>UL) is not executed. I checked the value of UP & it is greater than UL during the simulation. Any suggestion?
Thanks.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 17 日
編集済み: Azzi Abdelmalek 2014 年 4 月 17 日
What UL and other variables represent? If they are constants from workspace, use Ports and Data Manager
William
William 2014 年 4 月 17 日
UL, LL & UPI are all constants. I tried different opitons with Port and Data manager but the "if (UP>UL) is still not executed. BTW, does size matter (it is currently set at -1)? Thanks.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 17 日
What is the size of UL?
William
William 2014 年 4 月 17 日
UL equals to 18 & its sapling size is inf.
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 4 月 17 日
its sapling size is inf ? what is that ?
William
William 2014 年 4 月 17 日
I meant sampling time in the property box of constant block.
William
William 2014 年 4 月 17 日
Instead of using the 'elseif' statement, I created another 'if' statement & it worked. But there are some other issues I would like to resolve. I will post it later. Thanks again.
William
William 2014 年 5 月 19 日
編集済み: Azzi Abdelmalek 2014 年 5 月 19 日
function UP = UPC(t,LL,u1,u2,u3,u4,u5,u6,u7,u8)
%#codegen
persistent LLI
if isempty(LLI)
UP=LL;
else
UP=LLI;
end
My code
LLI=UP;
Azzi, I kept getting the error message "LLI is not defined..." when I attempted to run the simulation. "LL" is my IC for UP. Any idea? Thanks.

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

Danilo Teran
Danilo Teran 2018 年 12 月 4 日

1 投票

Hi
I have a similar problem, could you find a real solution.
Best regards?

カテゴリ

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

質問済み:

2014 年 4 月 17 日

回答済み:

2018 年 12 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by