要使用“audioread”读取不在 MatLab 路径中的音频文件,您必须指定完整的文件路径。
在您发布的代码中,不清楚您如何构建声音列表以及如何将其分配给列表框。
不过,可能的解决方案如下:您可以创建一个 (N x 2) 元胞数组,在其中存储每首歌曲的文件名及其路径。
您可以只为列表框指定文件名,然后您可以使用列表框中所选项目的索引来访问包含路径的单元格。
然后,您只需构建要在调用audioread 时使用的完整文件名(包括路径)。
此解决方案的实施取决于您如何管理声音列表。
为了测试这个解决方案,我构建了一个小型 GUI:我在“listbox1_CreateFcn”中手动创建了歌曲列表及其路径,并在 pushbutton1_Callback 中构建了完整的文件名,如下所示(我'已使用guidata 管理 GUI 中的声音列表):
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% Definition of the sound list
l_song{1,1}='song_1';
l_song{1,2}='c:\users\user_1\';
l_song{2,1}='song_2';
l_song{2,2}='c:\users\user_2\';
l_song{3,1}='song_3';
l_song{3,2}='c:\users\user_3\';
% Use guidata to manage the cellarray withn the GUI
my_guidata=guidata(gcf);
my_guidata.song_list=l_song;
guidata(gcf,my_guidata);
str=l_song(:,1)
set(hObject,'string',str)
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get idx of the selected sound
v=get(handles.listbox1,'Value')
% Get the list of song and paths
my_guidata=guidata(gcf);
l_song=my_guidata.song_list;
disp(['Song path= ' l_song(v,2)])
disp(['Song namw= ' l_song(v,1)])
% Build the full file name to be used in audioread
f_name=strcat(fullfile(l_song(v,2),l_song(v,1)),'.wav')
希望这会有所帮助。