How to select sections of signal using a for loop and waitfor?

3 views (last 30 days)
I want to select three sections from a signal, where subsequently the lowest and highest value of the x-axis of the zoomed section are stored in a matrix. Within the code below, I first plot the signal, and extract the figure and axis handles. Second, I use a for loop with three iterations (one for each section) in which I let the user zoom in on a specific section of the signal after a waitfor command (loop continues with an enter press by the user). Finally, the loop will extract the limits of the x-axis of the zoomed signal. However, the figure is closed by waitfor in the first iteration, and thereby removes the figure and axis handles. Within the second and third iteration, the limits of the full range are therefore extracted (of the non-zoomed figure).
%Initialization
Fs = 8000; % samples per second
dt = 1/Fs; % seconds per sample
StopTime = 0.25; % seconds
t = (0:dt:StopTime-dt)'; % seconds
%Sine wave:
Fc = 60; % hertz
x = cos(2*pi*Fc*t); % signal
%Plot the signal versus time:
figure(1);
plot(t,x); ylim([-1.5 1.5]); xlabel('time (in seconds)'); title('Signal versus Time');
%extract figure and axis handles
ax_units = zeros(3,2); %pre-allocate matrix
fig_pres = gcf;
ax_pres = gca;
%select sections and extract min and max values x-axis
for ii = 1:3
zoom on; %allows user to zoom in on specific section of signal
waitfor(fig_pres, 'CurrentCharacter', char(13)) %continue with enter press
ax_units(ii,:) = get(ax_pres, "Xlim"); %extract coordinates x-axis
zoom out %reset zoom level
zoom off
end
I have tried using the plot and handles within the for-loop, so it basically "remembers" the plot and handles, but this returned the same results:
%select sections and extract min and max values x-axis
for ii = 1:3
plot(t,x); ylim([-1.5 1.5]); xlabel('time (in seconds)'); title('Signal versus Time');
fig_pres = gcf;
zoom on; %allows user to zoom in on specific section of signal
waitfor(fig_pres, 'CurrentCharacter', char(13)) %continue with enter press
ax_pres = gca;
ax_units(ii,:) = get(ax_pres, "Xlim"); %extract coordinates x-axis
zoom out %reset zoom level
zoom off
end
How should I tackle this issue?
  1 Comment
dpb
dpb on 19 Aug 2022
Try pause instead (with a displayed instruction to the user, if somebody other than you might be using the routine) or uiwait

Sign in to comment.

Accepted Answer

Voss
Voss on 19 Aug 2022
Edited: Voss on 19 Aug 2022
I don't think waitfor is closing the figure. Perhaps the figure is closed by subsequent code not seen here, or maybe what you are seeing is the command window gaining focus and obscuring the figure when the enter key is pressed.
In any case, the problem is that, on the second and third iterations of the loop, the figure's 'CurrentCharacter' is already char(13), since that was the last key pressed, so waitfor doesn't wait, and the unzoomed xlimits are stored in ax_units(2,:) and ax_units(3,:).
Note the relevant documentation for waitfor: "If the specified property is already equal to propvalue, then waitfor returns immediately and execution resumes." (Source: waitfor)
To avoid this, set the 'CurrentCharacter' to something besides char(13) immediately before calling waitfor, e.g.:
%select sections and extract min and max values x-axis
for ii = 1:3
zoom on; %allows user to zoom in on specific section of signal
set(fig_pres,'CurrentCharacter',char(0)); % reset to char(0), so that waitfor waits for char(13)
waitfor(fig_pres, 'CurrentCharacter', char(13)) %continue with enter press
ax_units(ii,:) = get(ax_pres, "Xlim"); %extract coordinates x-axis
zoom out %reset zoom level
zoom off
end
Of course, the command window will still get focus on pressing the enter key and possibly obscure the figure, but you can switch back to it and continue the xlimits selection.

More Answers (0)

Categories

Find more on Visual Exploration in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!