MATLAB: How to add new audio devices after I have already created an object in the Audio System Toolbox

addaudioAudio Toolboxaudiodevicereaderaudiodevicewriteraudioplayerrecorderdevice

Sometimes when I am using the Audio System Toolbox, I need to add a device midway through a session of MATLAB, but when I add the new device I can't get MATLAB to recognize it even though my computer says that it is connected. So far, the only way I have been able to get the new device recognized is to restart MATLAB, which is not ideal. See the following code for an example:
>> deviceReader = audioDeviceReader;
>> devices = getAudioDevices(deviceReader); % shows 4 devices
>> % Connect new device
>> clear
>> deviceReader = audioDeviceReader;
>> devices = getAudioDevices(deviceReader); % still shows 4 even though 5 are connected
Is there a way that I can get MATLAB to recognize new devices without having to restart?

Best Answer

The device information is stored in memory in the same way a MEX file would be, so if you want to add new devices you first need to clear that information from MATLAB's memory. The following example code should allow you to add new devices without restarting MATLAB:
>> deviceReader = audioDeviceReader;
>> devices = getAudioDevices(deviceReader); % shows 4 devices
>> % Add new device
>> clear deviceReader dspAudioDeviceInfo
>> deviceReader = audioDeviceReader;
>> devices = getAudioDevices(deviceReader); % shows 5 devices
Note that you could also accomplish this using the "clear mex" command instead of specifically clearing "dspAudioDeviceInfo".
You can check if "dspAudioDeviceInfo" is currently saved in memory using the following command:
>> [~,X] = inmem