How do functions with multiple outputs work?

조회 수: 1 (최근 30일)
Brando Miranda
Brando Miranda 2016년 4월 28일
댓글: Star Strider 2016년 4월 28일
I was trying to write matlab functions but I sometimes find myself doing something really silly(in my opinion). Consider a function of multiple outputs:
function [ F_X, Z, A ] = f(obj,X)
F_X = magic(5);
Z = ones(5);
A = 2*ones(5);
end
and then I have some wrapper function like:
function [ F_X ] = get_first_arg(obj,X)
[F_X, ~, ~] = obj.f(X);
end
to get the first argument so that when I want to do use only the first argument I do
g(obj.get_first_arg(X))
instead of:
[F_X, ~, ~] = obj.f(X)
g(F_X)
this seems really silly. I have tried googling for this but didn't find anything I could use. How does matlab know that I only want the first object/matrix/data thing in output tuple?

답변 (1개)

Star Strider
Star Strider 2016년 4월 28일
If you just call your function as:
F_X = f(obj,X);
by default, only the first argument will be returned. You only need the tilde (~) if you do not want to return the first argument, for example:
[ ~, Z, A ] = f(obj,X);
  댓글 수: 2
Brando Miranda
Brando Miranda 2016년 4월 28일
Where is documentation discussing this?

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by