Hi,
I was facing this problem in inputing a vector to my user defined function. I have defined a function that needs two inputs to perform some operations. However, when it try to pass on a vector containing two element to the function it shows need more arguments error.
x = [2 , 4]
y = sq(x)
function out = sq(a , b)
out = a.^2 + b;
end
Can somebody show me how to do it right?
I will be very grateful.
Thanks a lot!

 채택된 답변

Alan Stevens
Alan Stevens 2020년 12월 5일

2 개 추천

Either
x = [2 , 4]
y = sq(x(1),x(2))
function out = sq(a , b)
out = a.^2 + b;
end
or
x = [2 , 4]
y = sq(x)
function out = sq(x)
a = x(1); b = x(2);
out = a.^2 + b;
end

추가 답변 (1개)

Sergey
Sergey 2024년 9월 2일

0 개 추천

Use evalc
func = @(a, b) a+b
func = function_handle with value:
@(a,b)a+b
InputArgumentsNumeric = [1, 2]
InputArgumentsNumeric = 1x2
1 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
InputArgumentsString = strjoin(cellstr(num2str(InputArgumentsNumeric')),',')
InputArgumentsString = '1,2'
[~,output] = evalc("func(" + InputArgumentsString + ")")
output = 3

카테고리

도움말 센터File Exchange에서 Time Series Collections에 대해 자세히 알아보기

제품

태그

질문:

JPS
2020년 12월 5일

답변:

2024년 9월 2일

Community Treasure Hunt

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

Start Hunting!

Translated by