MATLAB: Audio file (.wav) being overwritten

audio fileaudiowriteMATLABoverwritewav

I find that if I run the code that the audio file becomes overwritten so I then need to reload the original data into the directory .
What should I do ?
% y= Audio data
% Fs = Sample rate
% How to read the first "2" seconds
% samples = [1,2*Fs];
% clear y Fs
% [y,Fs] =audioread(filename,samples);
clear
clc
speech = ('coch4.wav');
speech2 = speech;
[y,Fs]=audioread(speech2); %don't use wavread or audioread
audiowrite(speech, speech_trimmed, 44100);
% Q1 a)
speechL = speech(:,1); % Should hear "Sometimes I whisper quietly and sometimes I shout very,very loud"
speechR = speech(:,2); % just noise
disp(Fs);
%Q1 b)
% Q1 c)
n = size(speech);
% Q1 d) https://au.mathworks.com/matlabcentral/answers/223670-how-can-i-change-the-sampling-frequency-of-audio-signal
Fs4 = Fs/4;
audiowrite(speech,speech_trimmed,Fs4);
[y,Fs4]=audioread(speech);
n2 = size(speech);

Best Answer

You have
speech = ('coch4.wav');
speech2 = speech;
So your speech2 filename is the same as your speech filename.
[y,Fs]=audioread(speech2); %don't use wavread or audioread
So you read from coch4.wav via speech2 filename
audiowrite(speech, speech_trimmed, 44100);
and then you write to the file name given by speech, which is the same as the filename given by speech2, so you are writing to the same file that you read the data from.