MATLAB Answers

What is the meaning of ~ at fuction?

85 views (last 30 days)
at 'Software-Analog Triggered Data Capture' document,
there's startCapture(hObject, ~, hGui) or endDAQ(~, ~, s)
What is the meaning of ~ at fuction?
I don't understand the meaning..
function startCapture(hObject, ~, hGui)
if get(hObject, 'value')
% If button is pressed clear data capture plot
for ii = 1:numel(hGui.CapturePlot)
set(hGui.CapturePlot(ii), 'XData', NaN, 'YData', NaN);
end
end
end
function endDAQ(~, ~, s)
if isvalid(s)
if s.IsRunning
stop(s);
end
end
end

  2 Comments

Bhaskar R
Bhaskar R on 27 Dec 2019
Notthing is passing/getting to function, no need of varible initialization
Hayeong Oh
Hayeong Oh on 27 Dec 2019
Thanks, but I still don't understand..
What is the difference of
endDAQ(~, ~, s) vs. endDAQ(s)
?

Sign in to comment.

Accepted Answer

Stephen Cobeldick
Stephen Cobeldick on 27 Dec 2019
Edited: Stephen Cobeldick on 27 Dec 2019
MATLAB has positional function input and output arguments. This means their purpose is determined entirely by their position when the function is defined and called, not by their names (like in some other programming languages).
It is useful sometimes to ignore particular inputs/outputs, which is what the tilde ~ is for. When defining a function, the tilde ignores a particular input, e.g. using your example the first function has three input arguments, of which only one is used:
function endDAQ(~, ~, s)
% ^ 1st input is ignored
% ^ 2nd input is ignored
% ^ 3rd input is named 's'
vs. your second function which only has one input argument:
function endDAQ(s)
% ^ 1st input is named 's'
Read more:

  5 Comments

Show 2 older comments
Hayeong Oh
Hayeong Oh on 31 Dec 2019
OK, I understand ~ is ignoring the variable.
But, in the endDAQ function, why is there three argument input?
inside the endDAQ function, there's only s variable..
What was the variable at the position of first and second?
I guess that can be (src, event, s) but, why?
function endDAQ(~, ~, s)
if isvalid(s)
if s.IsRunning
stop(s);
end
end
end
Stephen Cobeldick
Stephen Cobeldick on 31 Dec 2019
"What was the variable at the position of first and second?"
All MATLAB callback functions are called with the first two arguments being source and event:
(of course their exact names are not important, as MATLAB has positional arguments).
"I guess that can be (src, event, s) but, why?"
Because those first two input arguments are always supplied when the function is called. The documentation also shows how it is possible to supply the callback with more input arguments, such as by using this syntax:
... 'callback', {@endDAQ, s} ...
which, as the documentation explains, then provides three input arguments when the function is actually called: source, event, and s. So the answer to your question is "by definition".
What the function does with those three arguments is up to the code's author: if they want to ignore some of those input arguments (remember that source and event are always provided), then they can ignore any of those inputs using tildes.

Sign in to comment.

More Answers (0)

Sign in to answer this question.