Effacer les filtres
Effacer les filtres

declare a function in matlab

1 vue (au cours des 30 derniers jours)
marwa marwan
marwa marwan le 7 Août 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;

Réponses (2)

Azzi Abdelmalek
Azzi Abdelmalek le 7 Août 2015
Modifié(e) : Azzi Abdelmalek le 7 Août 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 le 7 Août 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.

Catégories

En savoir plus sur Argument Definitions dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by