Pregunta

In the picture, a part of my Simulink model is shown.

Part of the simulink model

It works like this:

  • Data is what is sent from serialport. When I start the simulation, if there is data sent, Data has the value of it. When there is not any data sent, Data sends 0 as output.

  • Status is 1 when there is data on serialport and 0 when there is not any data sent.

  • What I want it to do is: "If there is any data in serialport, give y the value of sent data. If there is no data sent keep, y as the previous value".

So I added my own User Defined Function

function y = fcn(u,x)
   if (x == 0)
      y = y;
   else
      y = u;
   end 
end

But this gives me the error says y is not defined. How can I achieve this simple solution wiht or without any user defined function? Can somebody, please, figure it out? Thanks in advance.

¿Fue útil?

Solución

Instead of writing this function, I would use an enabled subsystem with the default settings from the library (no block in it). When enable is zero, the output is held.

Otros consejos

Edit: The "not equal is actually "~=", as stated in another comment.

It seems to me that you're missing brackets from your function definition:

function [y] = fcn(u,x)
if (x == 0)
    y = y;
else
    y = u;
end 
end

Its fine tuning, but I think that could be done a little more beautifully. Here is my untested version:

function [y] = fcn(u,x)
//% "!=" means "not equal"
//% So when x isn't 0 the amount of u is added to y
if (x != 0)
    y = u;
end 
end
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top