フィルターのクリア

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.

3 ビュー (過去 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 Center および File ExchangeInteractive 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