【问题标题】:Choosing a file in listbox Matlab GUI在列表框 Matlab GUI 中选择文件
【发布时间】:2016-10-01 15:52:57
【问题描述】:

我有一个 Matlab GUI 列表框,它显示文件夹中的每个文件和一个按钮。如果我选择列表框中显示的文件之一并单击按钮,则该程序应该运行。 这是我的代码:

 allfiles = dir; %get files
 allname = {allfiles(~[allfiles.isdir]).name};
 set(handles.wavlist, 'String', allname);

 function wavlist_Callback(hObject, eventdata, handles)
     filenames = get(hObject, 'String');
     filechoice = get(hObject, 'Value');
     fileselected = wavnames{filechoice};

 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)
     [y, fs, nbits, opts] = wavread(fileselected);

但我收到错误未定义的函数或变量“文件选择”。任何想法?谢谢...

【问题讨论】:

  • 如果以下答案对您有用,请告诉我们...

标签: matlab user-interface listbox


【解决方案1】:

在函数tab1button_Callback 中未声明变量fileselected。 您必须声明 fileselected 全局,甚至更好地将其放入 handles 结构中,这是所有函数的参数。此结构旨在存储用户数据。

allfiles = dir; %get files
allname = {allfiles(~[allfiles.isdir]).name};
set(handles.wavlist, 'String', allname);

function wavlist_Callback(hObject, eventdata, handles)
 filenames = get(hObject, 'String');
 filechoice = get(hObject, 'Value');
 handles.fileselected = wavnames{filechoice};

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)
 [y, fs, nbits, opts] = wavread(handles.fileselected);

【讨论】:

  • 谢谢布什米尔斯。但它仍然说引用不存在的字段'selected_file'。你对此有什么想法吗?
  • 其实我想处理一个WAV音频文件。发生这种情况是因为我没有包含 .wav 扩展名吗?
  • 变量selected_file不会在您的代码 sn-p 中退出。你能解释一下你在哪里使用它吗?你是对的,如果你不包含文件扩展名,MATLAB 将找不到该文件。
  • 我的错,我的意思是handles.fileselected 不是selected_file。但最后通过添加 guidata(hObject, handles); 解决了。在handles.fileselected = wavnames{filechoice}之后;非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2012-04-19
  • 2010-10-21
  • 1970-01-01
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-18
相关资源
最近更新 更多