assigning argument of function within function

9 visualizzazioni (ultimi 30 giorni)
Salman Ahmad
Salman Ahmad il 10 Mag 2022
Risposto: Davide Masiello il 10 Mag 2022
% 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 Commento
Jan
Jan il 10 Mag 2022
Modificato: Jan il 10 Mag 2022
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.

Accedi per commentare.

Risposte (1)

Davide Masiello
Davide Masiello il 10 Mag 2022
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

Categorie

Scopri di più su Performance and Memory in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by