Not enough input arguments (line 2)

조회 수: 10 (최근 30일)
meman4567
meman4567 2023년 1월 24일
댓글: Stephen23 2023년 1월 24일
Using the two functions I have defined so far (nonsense and printstuff) to run the for loop at the end. When running the for loop, it turns me back to the nonsense .m file and says 'not enough input arguments' (line2). Can anyone help with this?
NONSENSE:
function y=nonsense(x)
if mod(x,2) == 0
disp(x^2);
else
disp(0);
end
end
PRINTSTUFF:
function printstuff(func,values)
fprintf('Function evaluated at %d points: \n',length(values))
for q = 1:length(values)
fprintf('point %0.6f has value %0.6f\n',values(q),func(values(q)))
end
end
FOR LOOP:
printstuff(nonsense,[4 11 15 20])
for x = [4 11 15 20]
nonsense(x);
end
  댓글 수: 1
Stephen23
Stephen23 2023년 1월 24일
편집: Stephen23 2023년 1월 24일
The function NONSENSE requires one input argument when it is called.
Here you are calling NONSENSE with zero input arguments:
printstuff(nonsense,[4 11 15 20])
% ^^^^^^^^ calling with zero input arguments.
What do you expect that to achieve? It seems that you need to use a function handle.

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

채택된 답변

Stephen23
Stephen23 2023년 1월 24일
편집: Stephen23 2023년 1월 24일
You need to learn about function handles:
I also had to fix your code because you had not defined the output argument "y" inside NONSENSE().
% v !!! define function handle !!!
printstuff(@nonsense,[4,11,15,20])
Function evaluated at 4 points: point 4.000000 has value 16.000000 point 11.000000 has value 0.000000 point 15.000000 has value 0.000000 point 20.000000 has value 400.000000
function y = nonsense(x)
if mod(x,2) == 0
y = x^2;
else
y = 0;
end
end
function printstuff(func,values)
fprintf('Function evaluated at %d points: \n',length(values))
for q = 1:length(values)
fprintf('point %0.6f has value %0.6f\n',values(q),func(values(q)))
end
end
  댓글 수: 2
meman4567
meman4567 2023년 1월 24일
Thanks for your answer. The ampersand did the trick (defining the function handle?)
Will definitely spend some time looking at the function handles: i'm very new to matlab so the more info I can take in the better :)
Not sure if info on this is in the documentation, but what exactly does the @ do?
Stephen23
Stephen23 2023년 1월 24일

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

추가 답변 (1개)

Arif Hoq
Arif Hoq 2023년 1월 24일
x=12:10:42;
out=nonsense(x);
144 484 1024 1764
function y=nonsense(x)
if mod(x,2) == 0
y=x.^2;
else
y=0;
end
disp(y)
end
  댓글 수: 2
meman4567
meman4567 2023년 1월 24일
Thanks for your answer. That code runs just fine. Now, I am using the following function
function printstuff(func,values)
fprintf('Function evaluated at %d points: \n',length(values))
for q = 1:length(values)
fprintf('point %0.6f has value %0.6f\n',values(q),func(values(q)))
end
end
Then using the two functions I have defined so far (nonsense and printstuff) to run this. This is where it turns me back to 'not enough input arguments'
printstuff(nonsense,[4 11 15 20])
for x = [4 11 15 20]
nonsense(x);
end
Arif Hoq
Arif Hoq 2023년 1월 24일
You can do it with a single function.
x = [4 11 15 20];
% x=[4 8 12 16 20];
out=nonsense(x);
Function evaluated at 4 points: point 4.000000 has value 16.000000 point 11.000000 has no value 0.000000 point 15.000000 has no value 0.000000 point 20.000000 has value 400.000000
function y=nonsense(x)
fprintf('Function evaluated at %d points: \n',length(x))
for i=1:length(x)
if mod(x(i),2) == 0
y=x(i).^2;
fprintf('point %0.6f has value %0.6f\n',x(i),y)
else
y=0;
fprintf('point %0.6f has no value %0.6f\n',x(i),y)
end
end
end

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

카테고리

Help CenterFile Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by