Wait for a condition to be met...

20 views (last 30 days)
Calvin Hobbes
Calvin Hobbes on 21 Oct 2017
Edited: Calvin Hobbes on 21 Oct 2017
Dear community, apologies for asking a simple and multiple-times-asked question...
[BatchAbf.x,~]=ginput(nIn);
% some lines are executed...
waitfor(numel(BatchAbf.x)=nIn);
% do not execute from here down until above condition is met
I know i can get away with pause and press enter, but this solution is more suited.
Any help greatly appreciated
  2 Comments
KL
KL on 21 Oct 2017
How and when does BatchAbf.x get nIn elements?
Calvin Hobbes
Calvin Hobbes on 21 Oct 2017
Edited: Calvin Hobbes on 21 Oct 2017
BatchAbf.x gets the (index) output of ginput function. nIn is a number (1 or 2 or ...) that determines the number of inputs ginput demands.
In other words if nIn = 2. I click twice on a graph, BatchAbf.x will get two index values, and thusly numel(BatchAbf.x) = nIn

Sign in to comment.

Accepted Answer

Jan
Jan on 21 Oct 2017
But ginput(n) waits already until the user has pressed the key n times. Then
if nIn = 2. I click twice on a graph, BatchAbf.x will get two index values, and
numel(BatchAbf.x) = nIn
is solved by the posted code already:
[BatchAbf.x, ~] = ginput(nIn);
Then what exactly is the problem?
  2 Comments
Calvin Hobbes
Calvin Hobbes on 21 Oct 2017
True that! I need to explain it a lot better, and now i understand what KL meant by their question...
GUIDE generated SignalOverview contains plots and pushbuttons.
Upon pushbutton1 click, RejectPeak is called.
If the user clicks on (in this instance) object handles.axes2 then numInd function that contains the ginput is called. Problem is that the moment i press the pushbutton1 the code runs to do something with BatchAbf.x before ginput is even engaged...
function varargout = SignalOverview(varargin)
% ...
function pushbutton1_Callback(hObject, eventdata, handles)
RejectPeak
end
end
function RejectPeak
% .....
function varargout = numInd(varargin)
[BatchAbf.x,~]=ginput(nIn);
end
set(handles.axes2,'ButtonDownFcn', @numInd);
waitfor(numel(BatchAbf.x)=nIn);
% do something with BatchAbf.x
end
Thank you for your time!
Jan
Jan on 21 Oct 2017
I do not understand the purpose of the code. Do you mean:
function RejectPeak
...
[BatchAbf.x,~]=ginput(nIn);
% do something with BatchAbf.x
end
What is the reason for setting a ButtonDown function, which calls a nested function, when this function can be called directly?
Or perhaps you want:
function RejectPeak
set(handles.axes2,'ButtonDownFcn', @numInd);
end
function varargout = numInd(varargin)
[BatchAbf.x,~]=ginput(nIn);
% do something with BatchAbf.x
end

Sign in to comment.

More Answers (2)

Calvin Hobbes
Calvin Hobbes on 21 Oct 2017
Edited: Calvin Hobbes on 21 Oct 2017
Dear all, thank you all for your input. Problem solved. I was trying to do something very simple in a convoluted manner, while loop could also work elegantly, however i went for the simplest solution, taking advantage of indeed the fact the ginput waits for completion beore executing subsequent code. The real problem i was trying to overcome was to get the user to first click on the desired (one of many) plot in order to get the correct handle, and then initiate the rest...
set(handles.axes2,'ButtonDownFcn', 'BatchAbf.axH=1'); % not getting the handle but a value that is meaningful for indexing
set(handles.axes2,'ButtonDownFcn', 'BatchAbf.axH=2'); % not getting the handle but a value that is meaningful for indexing
while isempty(BatchAbf.axH)==1
waitforbuttonpress; % code waits for correct user click
end
[BatchAbf.x,~] = ginput(nIn);

Categories

Find more on Startup and Shutdown 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!