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)
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');

Answers (1)

Daniel Vieira
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
Daniel Vieira
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.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by