Borrar filtros
Borrar filtros

declare a function in matlab

1 visualización (últimos 30 días)
marwa marwan
marwa marwan el 7 de Ag. de 2015
Respondida: Walter Roberson el 7 de Ag. de 2015
I have declare the function for check the limites and I call it but this error is appear
"Function definitions are not permitted at the prompt or in scripts."
this is the code
% call function
apos=bound(apos);
function p=bound(pp)
if(pp(1,1)<mina)
pp(1,1)=mina;
if(pp(1,1)>maxa)
pp(1,1)=maxa;
end
end
if(pp(1,2)<minb)
pp(1,2)=minb;
if(pp(1,2)>maxb)
pp(1,2)=maxb;
end
end
p=pp;

Respuestas (2)

Azzi Abdelmalek
Azzi Abdelmalek el 7 de Ag. de 2015
Editada: Azzi Abdelmalek el 7 de Ag. de 2015
You need to know how to call a function. Youcan't run it as a script. If you have for example this function
function y=fcn(u)
y=u.^2
Save it as fcn.m, then to use this function just write
a=2
out=fcn(a)

Walter Roberson
Walter Roberson el 7 de Ag. de 2015
You must store from "function p=bound" on to the end in a file named bound.m . With that version of the code you would also need to have mina, minb, maxa, and maxb be functions that return scalar values.
Or you could recode without "if" statements.
bound = @(pp) [min(max(pp(1),mina),maxa), min(max(pp(2),minb),maxb)];
which would be an executable statement that you could put in any time after you initialize mina, minb, maxa, maxb and before you call upon bound.

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by