Matlab Function block code

19 visualizaciones (últimos 30 días)
John Whelan
John Whelan el 24 de Mayo de 2017
Comentada: Les Beckham el 5 de Jun. de 2017
Hi, I am new to Matlab. I have a merge block that outputs either a 1 or 0. What I want to do is; If the input to the function block is 0, i want the output to be 0 and not be able to change back to 1. if the input is 1 i want the output to be a 1 but be able to change to a 0. is it possible to implement this? thank you in advance!

Respuesta aceptada

Honglei Chen
Honglei Chen el 24 de Mayo de 2017
I think you can use persistent variable to set a flag in the function block. If the input is ever 0, then you just ignore the future input. Something like this
function y = fcn(u)
persistent flag
if isempty(flag)
flag = true;
end
if flag
y = u;
if u == 0
flag = false;
end
else
y = 0;
end
end
HTH
  4 comentarios
Honglei Chen
Honglei Chen el 24 de Mayo de 2017
Editada: Honglei Chen el 24 de Mayo de 2017
You can try to use a switch block at the input, as shown here
so that one of the input is to your real input and the other one to a constant 1. For the condition, you can use a clock
and compare its output with the 0.5 threshold.
HTH
Walter Roberson
Walter Roberson el 24 de Mayo de 2017
Editada: Walter Roberson el 24 de Mayo de 2017
Add a Clock or Digital Clock block and run the input to the second port. Then,
function y = fcn(u, t)
persistent lastval
if isempty(lasval); lastval = cast(1, class(u)); end
if t <= 0.5
y = u;
else
y = cast(u & lastval, class(u));
lastval = y;
end

Iniciar sesión para comentar.

Más respuestas (1)

Les Beckham
Les Beckham el 25 de Mayo de 2017
Note that you can also do this with simple Simulink blocks (and no Matlab function block). If you don't have to protect against the input being zero during the first 0.5 seconds, it only takes two blocks. With the protection, it takes five. Here is an example. Be sure that you set the initial condition on the Memory block. You probably also want to make sure the decimation on the Clock block is set to 1.
<<
>>
  6 comentarios
Walter Roberson
Walter Roberson el 5 de Jun. de 2017
Editada: Walter Roberson el 5 de Jun. de 2017
I think you are right that
y = cast(u & lastval, class(u));
could be abbreviated as
y = u & lastval;
unless somehow the block was being called multiple times with different signal types (or unless somehow the signal type of the input can change during execution -- just because I can't think of a way that could happen doesn't mean it isn't possible.)
But mostly I think I put in the explicit typecast to be sure that code generation would know for sure what the output type would be, instead of code generation having to deduce that it is holding the correct type because of a persistent variable assigned to in the past.
Les Beckham
Les Beckham el 5 de Jun. de 2017
I agree completely that this is good 'defensive programming' practice.
I do like to encourage the use of built-in Simulink blocks where possible as I've found them more efficient and portable (across versions, etc.).
Of course, the original question was about a Matlab function block implementation. I just wanted to show that there was an alternative.

Iniciar sesión para comentar.

Categorías

Más información sobre General Applications en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by