Like we said in https://www.mathworks.com/matlabcentral/answers/394978-how-do-i-plot-the-sound-that-is-stored-in-the-variable-complete_sound#comment_557126 use audiowrite() to .wav file.
You might perhaps be thinking of recording what is played on the speaker. There is no way to "loop back" the audioplayer to audiorecorder. For a more detailed discussion of that topic see https://www.mathworks.com/matlabcentral/answers/386734-how-do-i-get-sound-data-from-a-speaker#answer_309137