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.
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Daniel Vieira
2022-12-8
编辑:Walter Roberson
2022-12-8
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 个评论
Walter Roberson
2022-12-8
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
2022-12-8
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!