Borrar filtros
Borrar filtros

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

1 visualización (últimos 30 días)
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!

Respuesta aceptada

Azzi Abdelmalek
Azzi Abdelmalek el 22 de Dic. de 2013
You can declare your variable as persistent
function y=fcn(u)
persistent v
if isempty(v)
v=your_constant
end
v=v+increment
  3 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 22 de Dic. de 2013
Editada: Azzi Abdelmalek el 22 de Dic. de 2013
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
Daniel
Daniel el 22 de Dic. de 2013
Actually I figured it out. No need for an answer. Thanks for brilliant answer!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Simulink Functions en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by