MATLAB: Issue with trying to play and record audio simultaneously

Audio ToolboxaudiotoolboxMATLAB

Hi guys, For my project I need to play a wav file from my speaker and while it's playing I want to record it with a microphone simultaneously. I'm using the audioPlayerRecorder which I believe is what I need to use. This is what I've done so far, however I'm not quite sure if its correct.
clear all
clc
filereader=dsp.AudioFileReader('chirp_2.wav'); % this is the wav file saved in my Computer
fs=filereader.SampleRate;
filewriter= audioDeviceWriter(fs);
apr=audioPlayerRecorder('SampleRate',fs);
while ~isDone(filereader)
audioToPlay = filereader();
audioRecorded = apr(audioToPlay);
filewriter(audioToPlay);
end
release(apr);
release(filewriter);
release(filereader);
mic1=getaudiodata(apr);
mic2=getaudiodata(filewriter);
When I run this, it runs fine so I'm assuming that both speaker and microphone are working synchronously?
My issue is, I want to plot both the audio from speaker and the recorded audio. I'm trying to convert the object to a numerical array through the getaudiodata function however having I get an error saying "Unrecognized function or variable getaudiodata()".
Does anyone know how to plot both the speaker and microphone audio?

Best Answer

getaudiodata() is only for audiorecorder() https://www.mathworks.com/help/matlab/ref/audiorecorder.html which is a routine that records audio and holds on to it until it is asked for.
You are using the system-object based routines such as audioPlayerRecorder. Those do not hold on to all the data: they only hold on to one buffer-worth. The audio data can be obtained by using step() -- or, exactly the way you did, by invoking the variable storing the object as-if it were a function. One buffer at a time.