MATLAB: Memory leak with ~ suppresed wavread function output

out of memorywavread

Why is the following code:
[~,~,~,opts]=wavread('example.wav');
causing apparent readout of all signal data into the memory? (memory is freed immediately upon completion making it hard to spot) This causes 'Out of memory' error when attempting to only read a small 'opts' parameters portion from a large .wav file.
Quote from help: Using the tilde (~) operator, you can change the declaration of a function so that it ignores any one or more entries in the input argument list(…)This can help you to keep your workspace clear of variables you have no use for, and also help you conserve memory.
At the same time wavread('size') does not cause this type of problems.
The only workaround I found to tis problem is: [~,~,~,opts]=wavread('example.wav',0);
Kind regards
Michal

Best Answer

This is not a "memory leak", but the expected behaviour. The called function, here wavread does not and cannot have information about the omitted output variables. So the tilde ~ helps to conserve memory by immediately deleting the variable during the return from the called function, but inside the function the memory is still used and can cause a "Out of memory" error.
The workaround you've mentioned is efficient, if only the options are wanted:
[~,~,~,opts] = wavread('example.wav', 0)