【问题标题】:Matlab error in file path for a sound声音的文件路径中的 Matlab 错误
【发布时间】:2015-08-25 10:54:42
【问题描述】:

我正在使用 GUI matlab,我正在录制 wav 声音并将其保存在特定文件夹中。当我按下播放按钮时出现错误,错误是:在 MATLAB 路径中找不到指定的文件名。 这是录制按钮的一小部分:

 name=strcat(year,'-',month,'-',day,'-',hour,'-',min,'-',sec);
             fullpath=fullfile('c:\monitoringsystem',name);
             wavwrite(y,44100,fullpath);
             y=[];

播放代码:

allstrings = cellstr( get(handles.listbox1, 'String') );
curvalue = get(handles.listbox1, 'Value');
thisstring = allstrings{curvalue};
[q, Fs] = audioread(thisstring);
 soundsc(q,44100);

如何解决这个问题,将声音保存在特定文件夹中。

【问题讨论】:

    标签: matlab user-interface path runtime-error directory


    【解决方案1】:

    要使用“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')
    

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多