Wait until user press return key to continue script

7 views (last 30 days)
I have a script plotting a figure with some vertical lines. I then want the user to select some of the lines and output a table with each line's index. I need the script to pause until the user has selected all the lines they want (and press 'enter' to indicate they are finished, for example), and then resume the script to save the selected indices in a table.
Here is my script:
clear, clc, close all
figure
x = rand(1,41);
y = 1:41;
H(1)= plot(x,y,'r.');
H(2)= line([x(21) x(21)],[0 max(y)], 'LineWidth', 2, 'Color', 'k');
H(3)= line([x(3) x(3)],[0 max(y)], 'LineWidth', 2, 'Color', 'k');
H(4)= line([x(15) x(15)],[0 max(y)], 'LineWidth', 2, 'Color', 'k');
set(H, 'ButtonDownFcn', {@LineSelected, H})
% WANT TO PAUSE HERE: WAIT FOR USER TO PRESS RETURN KEY (FOR EXAMPLE)
% THEN RESUME WITH BUILDING THE FOLLOWING TABLE
IndInWorkSpace = reshape(IndInWorkSpace, 2, [])';
ipt_sec = array2table(IndInWorkSpace,'VariableNames',{'start','end'});
ipt_sec.duration = ipt_sec.end - ipt_sec.start;
function LineSelected(ObjectH,EventData, H)
set(ObjectH, 'LineWidth', 4);
set(H(H ~= ObjectH), 'LineWidth', 2);
% Get x and y data of the highlighted lines
ind = ObjectH.XData(1);
if evalin('base','exist(''IndInWorkSpace'',''var'')')
indArray = evalin('base','IndInWorkSpace');
indArray(end+1) = ind;
else
indArray = ind;
end
assignin('base', 'IndInWorkSpace', indArray);
end
Any help appreciated, many thanks!

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 23 Nov 2021
Another option is to use waitfor along with a WindowKeyPressFcn.
x = rand(1,41);
y = 1:41;
H(1)= plot(x,y,'r.');
H(2)= line([x(21) x(21)],[0 max(y)], 'LineWidth', 2, 'Color', 'k');
H(3)= line([x(3) x(3)],[0 max(y)], 'LineWidth', 2, 'Color', 'k');
H(4)= line([x(15) x(15)],[0 max(y)], 'LineWidth', 2, 'Color', 'k');
set(H, 'ButtonDownFcn', {@LineSelected, H})
set(gcf,'WindowKeyPressFcn', @(~,~) set(gcf,'UserData', true));
waitfor(gcf,'UserData')
IndInWorkSpace = reshape(IndInWorkSpace, 2, [])';
ipt_sec = array2table(IndInWorkSpace,'VariableNames',{'start','end'});
ipt_sec.duration = ipt_sec.end - ipt_sec.start;
function LineSelected(ObjectH,EventData, H)
set(ObjectH, 'LineWidth', 4);
set(H(H ~= ObjectH), 'LineWidth', 2);
% Get x and y data of the highlighted lines
ind = ObjectH.XData(1);
if evalin('base','exist(''IndInWorkSpace'',''var'')')
indArray = evalin('base','IndInWorkSpace');
indArray(end+1) = ind;
else
indArray = ind;
end
assignin('base', 'IndInWorkSpace', indArray);
end
  1 Comment
Butterflyfish
Butterflyfish on 23 Nov 2021
That works perfectly, thank you so much for your help!

Sign in to comment.

More Answers (1)

Benjamin Kraus
Benjamin Kraus on 23 Nov 2021
Edited: Benjamin Kraus on 23 Nov 2021
One option is to add a WindowKeyPressFcn that runs the rest of your code. Something like this:
x = rand(1,41);
y = 1:41;
H(1)= plot(x,y,'r.');
H(2)= line([x(21) x(21)],[0 max(y)], 'LineWidth', 2, 'Color', 'k');
H(3)= line([x(3) x(3)],[0 max(y)], 'LineWidth', 2, 'Color', 'k');
H(4)= line([x(15) x(15)],[0 max(y)], 'LineWidth', 2, 'Color', 'k');
set(H, 'ButtonDownFcn', {@LineSelected, H})
IndInWorkSpace = [];
set(gcf,'WindowKeyPressFcn', @keyPress)
function LineSelected(ObjectH,EventData, H)
set(ObjectH, 'LineWidth', 4);
set(H(H ~= ObjectH), 'LineWidth', 2);
% Get x and y data of the highlighted lines
ind = ObjectH.XData(1);
if evalin('base','exist(''IndInWorkSpace'',''var'')')
indArray = evalin('base','IndInWorkSpace');
indArray(end+1) = ind;
else
indArray = ind;
end
assignin('base', 'IndInWorkSpace', indArray);
end
function keyPress(~,EventData)
IndInWorkSpace = evalin('base','IndInWorkSpace');
IndInWorkSpace = reshape(IndInWorkSpace, 2, [])';
ipt_sec = array2table(IndInWorkSpace,'VariableNames',{'start','end'});
ipt_sec.duration = ipt_sec.end - ipt_sec.start;
assignin('base', 'ipt_sec', ipt_sec);
end

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by