Function returns mapping to another function variable

3 vues (au cours des 30 derniers jours)
JITHIN NALUPURAKKAL
JITHIN NALUPURAKKAL le 24 Nov 2020
Questions:
  1. When i have 2 returns in the below function why does the "ans" only shows the value of "c" and not "d" or "absx" or "absy" ?
function [absx,absy] = my_first_function1()
input = 3;
b = [1 2 4; 3 4 6; 4 2 3];
c = input*b;
d = inv(c);
absx = c;
absy = d;
c
d
end
>> my_first_function1
c =
3 6 12
9 12 18
12 6 9
d =
-0.0000 -0.0667 0.1333
-0.5000 0.4333 -0.2000
0.3333 -0.2000 0.0667
ans =
3 6 12
9 12 18
12 6 9
2. How can i use the "absx" and "absy" inside another function.
My intention is to map x to absx and y to absy
function my_first_subfunction1()
my_first_function1
x = absx
y = absy
disp('..........')
z = x;
z
w = y;
w
end
If i use the above code it tends to throw an error like
Unrecognized function or variable 'absx'.
Error in my_first_subfunction (line 3)
x = absx

Réponse acceptée

Voss
Voss le 24 Nov 2020
1. "ans" shows the value of "absx" because that is the first output argument from the function my_first_function1. To suppress "ans" showing on the command line, terminate the call to my_first_function1 with a semicolon, like this:
my_first_function1();
2. In order to use "absx" and "absy" somewhere else, you have to assign their values to variables in the caller workspace (i.e., use the output arguments that my_first_function1 returns):
function my_first_subfunction1()
[absx,absy] = my_first_function1();
% do what you want to do with absx and absy ...
% (note that they don't have to be called absx and absy here, you could do this just as well:
% [x,y] = my_first_function1();
% and then do what you want with x and y.)
end

Plus de réponses (0)

Catégories

En savoir plus sur Workspace Variables and MAT-Files dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by