Accessing variables from workspace into user-created function block in Simulink

Good afternoon,
As the title suggests, I defined some variable say, x in the workspace that has a number.
Using MATLAB function, I am trying to import that variable (just once!) and as simulation progress, under
certain condition, I want to increment that variable. However, since the function does not 'see' the variable x,
I end up declaring that variable inside the MATLAB Function Block. However, this keeps initializing the variable
and thus, I cannot increment it.
I need help to resolve this problem!

 채택된 답변

You can declare your variable as persistent
function y=fcn(u)
persistent v
if isempty(v)
v=your_constant
end
v=v+increment

댓글 수: 3

yes about that. I was looking around and I was messing with persistent as well. Would you mind explaining what exactly persistent does? I read the help doc it doesn't really ring a bell.. Thank you anyways!
At time =0 , v takes the value your_constant. And each iteration the variable v will be incremented. persistent allows to remember the value of v
Actually I figured it out. No need for an answer. Thanks for brilliant answer!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Naming Conventions에 대해 자세히 알아보기

제품

질문:

2013년 12월 22일

댓글:

2013년 12월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by