assigning argument of function within function

조회 수: 11 (최근 30일)
Salman Ahmad
Salman Ahmad 2022년 5월 10일
답변: Davide Masiello 2022년 5월 10일
% complete function
function f = fix(x)
% x is an array of 10 arguments
% these functions
% function1
% function2
% function3
% function4
% function5
% are already there.
%
% I have assigned an array 'x'; now i want to assign the following functions these arguments but ....
% i am getting an error the way i do is as below.
a=function1(x(1),x(2),x(4))
b =function2(x(2),x(5),x(10))
c = function3 (x(3))
d =function4 (x(10),x(9))
e=function5(x(6),x(7),x(8))
fix=a+b+c+d+e;
end
how can i do it.
  댓글 수: 1
Jan
Jan 2022년 5월 10일
편집: Jan 2022년 5월 10일
Which error message do you get? This information is important, so it is a good idea to share it with the readers.
What is "function1" etc.? I cannot guess this detail.
One typo:
fix=a+b+c+d+e;
% ^^^ No, not the name of the function, but "f", the output variable
By the way, "fix" is a built-in function. Shadowing it by a user-defined function can cause severe side-effects.

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

답변 (1개)

Davide Masiello
Davide Masiello 2022년 5월 10일
The error is in the last line of the function fix.
Change that and the code works.
fix(rand(1,10))
ans = 6.0073
% complete function
function f = fix(x)
% x is an array of 10 arguments
% these functions
% function1
% function2
% function3
% function4
% function5
% are already there.
%
% I have assigned an array 'x'; now i want to assign the following functions these arguments but ....
% i am getting an error the way i do is as below.
a=function1(x(1),x(2),x(4));
b =function2(x(2),x(5),x(10));
c = function3 (x(3));
d =function4 (x(10),x(9));
e=function5(x(6),x(7),x(8));
f=a+b+c+d+e;
end
function out = function1(a,b,c)
out = a+b+c;
end
function out = function2(a,b,c)
out = a+b+c;
end
function out = function3(a)
out = a;
end
function out = function4(a,b)
out = a+b;
end
function out = function5(a,b,c)
out = a+b+c;
end

카테고리

Help CenterFile Exchange에서 Performance and Memory에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by