Zgrkpnr
Zgrkpnr

Reputation: 1231

I need help for a very simple function in Matlab

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

Part of the simulink model

It works like this:

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.

Upvotes: 0

Views: 77

Answers (2)

Dreeri
Dreeri

Reputation: 61

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

Upvotes: 0

Daniel
Daniel

Reputation: 36710

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.

Upvotes: 2

Related Questions