Hi. In my main M script I have:
x = 0:50;
y = 0:50;
[X,Y] = meshgrid(x,y);
P = P_fn(X,Y);
in my function I have:
function P_fn(x,y)
P = <some equation involving both terms x and y>
end
when I run it I get the error:
>> P = P_fn(X,Y);
Error using P_fn
Too many output arguments.
Can functions only output one calculation and output? My equation works just fine if I use it in the main M script.
Thanks for reading

 채택된 답변

Star Strider
Star Strider 2017년 5월 14일

3 개 추천

The error is because you did not define the function with outputs.
Try this:
function P = P_fn(x,y)
P = <some equation involving both terms x and y>
end

댓글 수: 4

Alex Vasin
Alex Vasin 2017년 5월 14일
Thanks. That works now. The reason I omitted it is because I wanted to be able to have the output displayed in the Command Window as P = answer rather than ans = answer. There doesn't seem to be any other way to get rid of the "ans = " part of the displayed answer.
My pleasure.
You can still have ‘P’ display to the Command Window by simply removing the semicolon from the end of this line:
P = P_fn(X,Y)
If my Answer helped you solve your problem, please Accept it!
Hadjer BEN BRAHIM
Hadjer BEN BRAHIM 2021년 9월 14일
Please help me with my problem :
function a = myRand
a = 10*rand(3,4);
end
--------------
When I ran it
myRand ( it shows up)
Then I want to assign the result to a variable b :
b = myRand
And there is an error :
Error using myRand
Too many output arguments.
This should be a new Question.
b = myRand
b = 3×4
1.4722 0.2712 7.6027 8.2860 4.9290 5.6392 1.6085 8.6403 8.9170 5.5582 5.6037 7.0630
function a = myRand
a = 10*rand(3,4);
end
That said, it works for me, so problem solved!
.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Bartomeu Serra Gonzalez
Bartomeu Serra Gonzalez 2021년 11월 4일

0 개 추천

Mi función es la siguiente:
y a la hora de ejecutar, en un scrip:
me aparece el error:
¿Porqué?

댓글 수: 1

Star Strider
Star Strider 2021년 11월 4일
The ‘f’ function does not return anything, although the anonymous function within it does.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Scatter Plots에 대해 자세히 알아보기

태그

질문:

2017년 5월 14일

댓글:

2021년 11월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by