# add a function to a model

3 visualizzazioni (ultimi 30 giorni)
Néstor Monedero il 3 Nov 2011
Hi, I develop a function in matlab code:
function u = f(y)
if (u>0.8)
y=1;
elseif (u<0.8)
y=-1;
else
y=0;
where u is a random signal generate with the random number block. How can i join these two blocks? (random number and a block which contain my function without errors). I try using level-2 matlab s-function but it make error. can i use another block in which i introduce my matlab function code?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Fangjun Jiang il 3 Nov 2011
Use the "MATLAB Fcn" or "Fcn" block from library Simulink>User-Defined Functions.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Più risposte (2)

Daniel Baboiu il 3 Nov 2011
If a function is coded as function u = f(y), then y is the argument and u is the return value. Your block of code does it the other way around.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Néstor Monedero il 3 Nov 2011
Sorry, the matlab function code is:
function y = fnc(u)
if(u<0.8)
y=-1;
elseif (u>0.8)
y=1;
else
y=0;
end

Accedi per commentare.

Jon il 3 Nov 2011
Have you tried using a 'MATLAB Function' block? Under 'User-Defined Functions'.
u will be your input port and y will b your output port.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Néstor Monedero il 3 Nov 2011
Thanks, i use the matlab function block.

Accedi per commentare.

### Categorie

Scopri di più su Create MATLAB S-Functions in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by