declare a function in matlab
2 views (last 30 days)
Show older comments
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;
0 Comments
Answers (2)
Azzi Abdelmalek
on 7 Aug 2015
Edited: Azzi Abdelmalek
on 7 Aug 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)
0 Comments
Walter Roberson
on 7 Aug 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.
0 Comments
See Also
Categories
Find more on Function Creation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!