declare a function in matlab
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(2 个)
Azzi Abdelmalek
2015-8-7
编辑:Azzi Abdelmalek
2015-8-7
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 个评论
Walter Roberson
2015-8-7
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Function Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!