title: MATLAB: Having audioplayer() continue to play after function ends
body: <p>I am using code that uses the following subfunction:</p>
  
  <pre><code>function playTone (duration, toneFreq)
  % Generate a tone
  
  samplesPerSecond = 44100; % the bit rate of the tone
  y = sin(linspace(0, duration * toneFreq * 2 * pi, round(duration * samplesPerSecond))); % the equation of the sound wave
  player = audioplayer(y, samplesPerSecond); % create an audio object from the sound wave at the specified bit rate
  playblocking(player) % play the audio, blocking control until the sound completes
  </code></pre>
  
  <p>This function is called, for example, with the following:</p>
  
  <pre><code>playTone(4, 400);
  </code></pre>
  
  <p>This causes a sound to play at 400Hz, lasting for 4 seconds.</p>
  
  <p>The problem is, the function playblocking() restricts control until the sound has completed. The alternative is to use play(), which means that no sound plays at all (because sound stops as soon as the function completes).</p>
  
  <p>I can't use the sound() function due to a known bug in my version of MATLAB... How can I make the audioplayer() function play a sound without taking control of the system, if the sound is created within a subfunction?</p>