필터 지우기
필터 지우기

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.

조회 수: 4 (최근 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');

답변 (1개)

Daniel Vieira
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
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
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 CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by