What is the meaning of ~ at fuction?

2 次查看(过去 30 天)
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 个评论
Bhaskar R
Bhaskar R 2019-12-27
Notthing is passing/getting to function, no need of varible initialization
Hayeong Oh
Hayeong Oh 2019-12-27
Thanks, but I still don't understand..
What is the difference of
endDAQ(~, ~, s) vs. endDAQ(s)
?

请先登录,再进行评论。

采纳的回答

Stephen23
Stephen23 2019-12-27
编辑:Stephen23 2019-12-27
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 个评论
Stephen23
Stephen23 2019-12-31
编辑:Stephen23 2019-12-31
"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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by