How to write a Matlab code that shall be executed only once - Embedded MATLAB Function

Hello,
I am writing a code inside my simulink model using Embedded MATLAB Function. Here I have a small part of code which I want to execute only once in runtime at startup. But I do not know how to do it? Please help me. The flag set technique is not helping me as i get error.
Following piece of my variable init I would like to execute only once. Could you please suggest me some methods to make it so.
Counter = 0;
RotateDir = 0;
Reset = 1;
InitRunOver = 0;
Thanks.

1 comentario

can writte the full function: function .... (input & output of your function)

Iniciar sesión para comentar.

Respuestas (2)

If you need just to make initialization, you can use
  • model properties
  • callbacks
  • init fcn: then writte in your code
Hello
Thanks, I will try your method. I will make my question more specific.
Following is my matlab code. Here how do I make the first few init lines to execute only once. Is it by putting into callbacks>init fcn? Please guide me.
function [RotateDir,Reset] = fcn(LightSensorA, LightSensorB)
Counter = 0;
RotateDir = 0;
Reset = 1;
InitRunOver = 0;
a = 2;
if ((LightSensorA == 1)&&(Counter == 0))
RotateDir = 0;
elseif (LightSensorA == 0)
RotateDir = 1;
Counter = 0;
end
if (RotateDir == 1)
Counter = Counter+1;
end
if (LightSensorB == 0)
Counter = Counter/2;
InitRunOver = 1;
end
if(InitRunOver == 1)
if(Counter > 0)
Counter = Counter -1;
else
Reset = 0;
end
end

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 9 de Nov. de 2012

Community Treasure Hunt

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

Start Hunting!

Translated by