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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by