필터 지우기
필터 지우기

To run a function

조회 수: 1 (최근 30일)
Joy Tian
Joy Tian 2015년 7월 23일
댓글: Joy Tian 2015년 7월 24일
I have a question in my mind. Why we need to put square bracket containing all output arguments ahead of the function when running a function in the command window? Instead of directly calling the function.
Why only by typing the full command with the output arguments appearing at the beginning can we get the result?
  댓글 수: 2
John D'Errico
John D'Errico 2015년 7월 24일
Because that is how MATLAB syntax is defined? Software does what it is written to do, nothing more. MATLAB is just software.
Joy Tian
Joy Tian 2015년 7월 24일
Well, you are right, John. Well I still like Matlab because it is so good at matrix.

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

채택된 답변

Thorsten
Thorsten 2015년 7월 23일
편집: Thorsten 2015년 7월 23일
You can call a function w/o the square brackets in front, but then you only get the first return value in ans, as David explained. If there is more than one return value, these are NOT assigned to, say, ans2, ans3, ..., probably because Mathworks decided not to clutter your workspace with unwanted data. Keep in mind that there are functions with many return values. So Matlab basically gives you only what you've asked for, with ans as an exception.
  댓글 수: 1
Joy Tian
Joy Tian 2015년 7월 24일
I see. I will remember this.
Thank you Thorsten!

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

추가 답변 (1개)

David Young
David Young 2015년 7월 23일
If you call the function without a result variable, MATLAB assigns the result to the variable ans. That is
min([3 2 1 5 6])
is the same in effect as
ans = min([3 2 1 5 6])
If you want to assign the result to some other variable, you can name it in front of the equals sign, with or without square brackets round it:
y = min([3 2 1 5 6])
or
[y] = min([3 2 1 5 6])
You only need the square brackets if the function can return more than one result. If you want the second result (and any later ones), you need to use the brackets and name the variables you want to assign the result to, like this:
[y, idx] = min([3 2 1 5 6])
  댓글 수: 1
Joy Tian
Joy Tian 2015년 7월 24일
“You only need the square brackets if the function can return more than one result. If you want the second result (and any later ones), you need to use the brackets and name the variables you want to assign the result to”
That's it!
[y, idx] = min([3 2 1 5 6])
y =
1
idx =
3
Thank you so much, David!

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by