What does it mean when ~ is used as one of the outputs for a function?

조회 수: 6(최근 30일)
I have seen a few scripts where ~ is used as one of the output variables for a function/command? What is this supposed to mean and how is it useful? For example, in one of the lines for the script for the function divergence.m, I find the line [px, ~] = gradient(u);

채택된 답변

James Tursa
James Tursa 2016년 10월 28일
편집: James Tursa 2016년 10월 28일
It means that the particular output that would ordinarily be returned to a variable at that spot is instead discarded automatically. E.g.,
[px, ~] = gradient(u);
is essentially equivalent to:
[px, dummy] = gradient(u); clear dummy
  댓글 수: 2
James Tursa
James Tursa 2016년 10월 28일
All good points. I will add that the ~ does not prevent the function from actually calculating that particular output argument. It is in fact calculated (the function still thinks it needs to return two outputs) ... it is just automatically discarded per the syntax. So using ~ is more of a coding convenience rather than a resource or time saver. (Is there a way for the function to detect when the caller has used ~ for an output?)

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

추가 답변(1개)

LauraLee Austin
LauraLee Austin 2016년 10월 28일
It indicates that you are not using that output variable.
[FX,FY] = gradient(F)
Let's say you wanted the FY variable but not FX:
[~,FY] = gradient(F)


Find more on Loops and Conditional Statements in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by