Function returns mapping to another function variable

1 回表示 (過去 30 日間)
JITHIN NALUPURAKKAL
JITHIN NALUPURAKKAL 2020 年 11 月 24 日
コメント済み: JITHIN NALUPURAKKAL 2020 年 11 月 25 日
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

採用された回答

Voss
Voss 2020 年 11 月 24 日
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
  1 件のコメント
JITHIN NALUPURAKKAL
JITHIN NALUPURAKKAL 2020 年 11 月 25 日
Thanks so much for your input.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Identification についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by