Diplay wav files in listbox GUI

Hi, I need to make a listbox GUI that displays every wav files that I have in a folder. What I want is I just simply put the cursor in one of the wav files at the listbox and click a pushbutton to run my sound processing code. I have this function :
function wavlist_Callback(hObject, eventdata, handles)
function wavlist_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function tab1button_Callback(hObject, eventdata, handles)
Like this :
I've tried to put this code in my GUI code but I don't really know how to make it right :
allfiles = dir; %get files
allname = {allfiles(~[allfiles.isdir]).name}
Someone please help me...

 Accepted Answer

set(handles.wavlist, 'String', allname);
and inside wavelist_Callback
wavnames = get(hObject, 'String');
choice = get(hObject, 'Value');
selected_file = wavnames{choice};

9 Comments

Thank you so much Walter! Now I have this problem, say that my signal processing code is just to read the wave file, so I need to write the code below inside tab1button_Callback :
[y, Fs, nbits, opts] = wavread(selected_file);
Since I got error Undefined function or variable 'selected_file', I moved the code inside wavelist_Callback to tab1button_Callback, so it becomes like this :
function tab1button_Callback(hObject, eventdata, handles)
wavnames = get(hObject, 'String');
choice = get(hObject, 'Value');
selected_file = wavnames(choice);
[y, Fs, nbits, opts] = wavread(selected_file);
But when I chose a wave file that displayed in the listbox, I got another error Invalid Wave File. Reason: Cannot open file. Do you have any idea about this? I am so sorry to ask you such question.
Notice that I used wavnames{choice} not wavnames(choice)
But it says Cell contents reference from a non-cell array object. if I use {} not ()
wavlist_Callback not whatever that tablist is
Oh my bad. I actually want to process an audio file that I choose in the listbox by clicking the pushbutton. But when I did this :
function wavlist_Callback(hObject, eventdata, handles)
wavnames = get(hObject, 'String');
choice = get(hObject, 'Value');
selected_file = wavnames{choice};
[y, Fs, nbits, opts] = wavread(selected_file);
The program already run when I choose the file in the listbox before I even click the pushbutton. I guess I need to define selected_file as a global variable so it can be used inside the pushbutton function. But I just don't know how to do this. I tried this :
function wavlist_Callback(hObject, eventdata, handles)
wavnames = get(hObject, 'String');
choice = get(hObject, 'Value');
handles.selected_file = wavnames{choice};
function pushbutton_Callback(hObject, eventdata, handles)
[y, Fs, nbits, opts] = wavread(handles.selected_file);
I got error Reference to non-existent field 'selected_file'. Can you help me Walter? Thank you so much.
After
handles.selected_file = wavnames{choice};
add
guidata(hObject, handles);
It still say Reference to non-existent field 'selected_file'. Error in main>pushbutton_Callback (line 318) [y, Fs, nbits, opts] = wavread(handles.selected_file);
Make sure you do not press the pushbutton before you have used the listbox to select an item. Or, alternately, make sure that you define handles.selected_file even before the listbox callback has ever been run.
argh my bad, thank you so much Walter. you've been a great help!

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects 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!