i want to add a line when i press 'escape' button the program stops, because this program is infinite. Also what is the benefit of '"drawnow();" because i did not get it.
1 view (last 30 days)
Show older comments
close all
clear all
clc
%Define parameters
fs = 96000; % Sampling frequency
nBits = 24; % Number of bits
nChannels = 1; % Number of channels
duration = inf; % Duration of recording in seconds
%Create recorder object
recObj = audiorecorder(fs, nBits, nChannels);
disp('Start speaking:')
recObj.record(duration);
while recObj.isrecording()
pause(0.1);
plot(recObj.getaudiodata());
title('The recording')
xlabel('Time')
ylabel('Audio Signal')
drawnow();
end
disp('End of Recording');
0 Comments
Answers (1)
Daniel Vieira
on 8 Dec 2022
Edited: Walter Roberson
on 8 Dec 2022
in matlab, a plot is normally only drawn when the script ends, so if you are plotting and constantly overwritting you would see nothing until it finishes running. with drawnow, you force the plot to happen when at that moment, so you can see plots getting updated while the code runs.
as for the ESC thing, it may be too much work, I'll suggest something else. you can change your while loop to run until you close the figure window, like this:
fig=figure;
n=1;
while ishandle(fig)
n=n+1;
n
pause(0.2)
end
3 Comments
Walter Roberson
on 8 Dec 2022
You could waitbar(), and in your loop test the validity of the handle of the waitbar. The user would close the waitbar to stop the loop.
If you really want to do it by Escape key, then you need to create a WindowKeyPressFcn callback, and that callback needs to test the current key (information is in the second parameter to the callback function) to be sure it is the escape key (and not modified such as control-escape), and if so then set a flag that the loop is testing.
Daniel Vieira
on 8 Dec 2022
hmm, you could "wrap" your code in an app with appdesigner then, add a "play/pause" button and use it as the condition for your while loop. a little work but less than configuring a listener to the ESC button.
See Also
Categories
Find more on Dialog Boxes 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!