I need help for a very simple function in Matlab

조회 수: 1 (최근 30일)
Özgür Akpinar
Özgür Akpinar 2014년 3월 16일
답변: the cyclist 2014년 3월 16일
In the picture a part of my simulink model is shown. It works like this:
"Data" is what is sent from serialport. "Status" is 1 when there is data on serialport and 0 when there is not any data sent.
When I start simulation, if there is data sent "Data" has the value of it. When there is not any data sent "Data" sends 0 as output.
What I want it to do is "If there is any data, 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

답변 (1개)

the cyclist
the cyclist 2014년 3월 16일
You have not input the previous value of y to fcn(), so at the line
y = y;
the value is unknown. You need something like
function y = fcn(u,x,y0)
if (x == 0)
y = y0;
else
y = u;
end
end
where y0 is the prior value of y.

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by