필터 지우기
필터 지우기

declare a function in matlab

조회 수: 1 (최근 30일)
marwa marwan
marwa marwan 2015년 8월 7일
답변: Walter Roberson 2015년 8월 7일
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;

답변 (2개)

Azzi Abdelmalek
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)

Walter Roberson
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.

카테고리

Help CenterFile Exchange에서 Argument Definitions에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by