Calling a function from another function

3 vues (au cours des 30 derniers jours)
JITHIN NALUPURAKKAL
JITHIN NALUPURAKKAL le 24 Nov 2020
The first function is as below:
function my_first_function1(input,b)
c = input*b;
absx = c;
absx
end
The second function calling the first function is below:
function my_first_subfunction(a)
input = 2;
d = my_first_function1(input,b);
out = d +a;
out
end
Error:
>> my_first_subfunction(a)
Unrecognized function or variable 'b'.
Error in my_first_subfunction (line 3)
d = my_first_function1(input,b);

Réponse acceptée

James Tursa
James Tursa le 24 Nov 2020
my_first_function1( ) doesn't return any value to the caller. To return a value you use this syntax:
function absx = my_first_function1(input,b)
c = input*b;
absx = c;
absx
end
my_first_subfunction( ) uses variable b before it is defined. Examine the code for this function and you will see that there is nothing in the function that defines b before it is used as an input argument to another function.
  6 commentaires
JITHIN NALUPURAKKAL
JITHIN NALUPURAKKAL le 24 Nov 2020
function [absx,absy] = my_first_function1(input,b)
c = input*b;
d = inv(c);
absx = c;
absy = d;
absx
absy
end
function my_first_subfunction(input,b,absx,absy)
input = 3;
b = [1 2 4; 3 4 6; 4 2 3];
d = my_first_function1(input,b);
disp('b incomin')
z = absx;
y = absy;
disp(z);
disp(y);
end
>> my_first_subfunction
absx =
3 6 12
9 12 18
12 6 9
absy =
-0.0000 -0.0667 0.1333
-0.5000 0.4333 -0.2000
0.3333 -0.2000 0.0667
b incomin
Not enough input arguments.
Error in my_first_subfunction (line 6)
z = absx;
JITHIN NALUPURAKKAL
JITHIN NALUPURAKKAL le 24 Nov 2020
Really appreciate your insight.
I tried this way. But even though am passing the return to "my_first_subfunction", its not taking the variables absx and absy from "my_first_function1".

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by