필터 지우기
필터 지우기

How to use a function output in another function?

조회 수: 1 (최근 30일)
Larry
Larry 2014년 1월 25일
편집: Stephen23 2020년 9월 5일
I have written a function in the script, say
function A = matrixy()
A = zeros(3);
end
When calling it in the command window, I'll have
>>matrixy
ans =
0 0 0
0 0 0
0 0 0
But when I key in A in the command window, it shows "Undefined function or variable 'A'". Is there a way to let A pick up this value in the command window?
Also, I want to create another function and use this matrix A produced in the previous function. How should I do it?
Many thx!
  댓글 수: 1
Stephen23
Stephen23 2017년 8월 11일
편집: Stephen23 2020년 9월 5일
How to assign output arguments to variables is also covered in the introductory tutorials:
Doing the introductory tutorials teaches lots of these basic concepts, which saves time in the long run.

댓글을 달려면 로그인하십시오.

채택된 답변

Amit
Amit 2014년 1월 25일
편집: Amit 2014년 1월 25일
A = matrixy;
A function has an output and the name. Unless you define the output, it goes to ans variable.
  댓글 수: 2
Fernadez
Fernadez 2017년 8월 10일
I don't think Amit answer is correct, I am having the same question. if A = matrix, you are running the whole function all over again. if you run the function, how do you save the result "A", and how to you call "A" an input to the second function?
Walter Roberson
Walter Roberson 2017년 8월 10일
A = matrixy();
calls matrixy once (per time that you execute the statement), and assigns the output of running matrixy() the variable "A" in your current workspace.
You can then pass A to another routine, such as
sqrt(A)
If you do not need the output for second purposes, you can just directly call
sqrt(matrixy())

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by