I need to save the audio output of a script in Matlab but I don't know how to do it. It is a complex sound, with several pauses of silence with different durations, so I cannot simply use audiowrite (for my understanding, I need one single matrix S to save a file using audiowrite(S,Fs)). A simple example of how the sound is generated would be the following:

for ii = 1:length(cf) - 1
sound(s(ii,:), sf);           % sound presentation
pause(d + 0.025);             % waiting for sound end + silent period

where d is the sound duration and 0.025 is the duration of the silence. I was thinking in creating a vector describing the silent period (similarly to the matrix s describing the sound), but as it has a different duration with respect to the sound so the number of columns (which represents the number of signal samples) would be different.

Is it possible to record the output? I would like to save exactly what I hear in the speakers after running the script. It is my first time with sounds in Matlab and I did not find anything online. Thank you for your help.


1 Answer 1


Are you aware of cell-arrays in Matlab?

Consider the following short script-file,

    close all; clear; clc;

    x=randn(1024, 1);
    y=2+sqrt(0.5).*randn(2048, 1);

    S=[ {x}; {y} ]; size(S)

    z=[ S{1, :}; S{2, :} ];

    figure; ...
        subplot(3,1,1); ...
            plot(S{1, :}); grid on; xlim([1 numel(S{1, :})]);
        subplot(3,1,2); ...
            plot(S{2, :}, 'r'); grid on; xlim([1 numel(S{2, :})]);
        subplot(3,1,3); ...
            plot(z, 'k'); grid on; xlim([1 numel(z)]);

Once you create a vector of cell-arrays (each cell-array will have a different length that is dependent on the duration of the silent periods), you can save it to a file.

I hope this helps.


Not the answer you're looking for? Browse other questions tagged or ask your own question.