assigning argument of function within function

12 ビュー (過去 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 Center および File ExchangePerformance and Memory についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by