How can I read a .wav file and save it with other name by 2push buttons?

2 views (last 30 days)
Hi I want to read a wave file by a push button and save as it with other push button with a new name, how can I do that?
I do this code but have get Error!
% --- Executes on button press in Openb.
function Openb_Callback(hObject, eventdata, handles)
% hObject handle to Openb (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global filename pathname y fs nbits;
[filename, pathname]=uigetfile('*.wav', 'Select a wave file');
[handles.y, fs, nbits] = wavread(fullfile(pathname, filename));
% --- Executes on button press in Saveb.
function Saveb_Callback(hObject, eventdata, handles)
% hObject handle to Saveb (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global filename pathname y fs nbits;
[filename pathname]=uiputfile('*.wave', 'Choose a name to save file');
wavwrtie(y,fs,nbits,'*.wav');

Accepted Answer

Geoff Hayes
Geoff Hayes on 4 Dec 2014
Edited: Geoff Hayes on 4 Dec 2014
reza - you may want to describe what error you are getting (and where) but presumably it is in the Saveb_Callback function because y might be empty. Note how in Openb_Callback you are setting handles.y to be the samples from the audio file, and not the global variable y. Or there is an error because you have spelled the function name, wavwrite, incorrectly and are not supplying an appropriate filename.
Rather than using global variables, you should be using handles which can also be used to store user data. So in your first callback, do the following instead
function Openb_Callback(hObject, eventdata, handles)
[filename, pathname]=uigetfile('*.wav', 'Select a wave file');
[y, fs, nbits] = wavread(fullfile(pathname, filename));
% save the data to handles
handles.y = y;
handles.fs = fs;
handles.nbits = nbits;
guidata(hObject,handles);
Note how we use guidata to save the updated handles structure.
Now in your second callback, just reference the audio data through the handles structure as
function Saveb_Callback(hObject, eventdata, handles)
[filename pathname]=uiputfile('*.wave', 'Choose a name to save file');
wavwrite(handles.y,handles.fs,handles.nbits,fullfile(pathname,filename));
In the first callback, I didn't save the filename and pathname to the handles structure because it wasn't clear where the two were being used outside this function. If you do need them, then just add them to handles like the other variables.

More Answers (0)

Categories

Find more on Variables in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!