2012-12-14 18 views
0

我正在嘗試使用GUIDE構成的GUI填充彈出式菜單。我做如下:使用目錄文件填充彈出式菜單

TestFiles = dir([pwd '/test/*.txt']); 
TestList = []; 

for i = 1:length(TestFiles) 
    filename = TestFiles(i).name; 
    TestList = [TestList filename]; 
end 

set(handles.popup_test,'string',TestList); 

我這樣做的popup_test_CreateFcn方法中(我真的不知道這是雖然正確的地方)。

當嘗試啓動我不斷收到這樣的GUI:

??? Attempt to reference field of non-structure array. 

Error in ==> init>popup_test_CreateFcn at 101 
set(handles.popup_test,'string',TestList); 

Error in ==> gui_mainfcn at 96 
     feval(varargin{:}); 

Error in ==> init at 19 
    gui_mainfcn(gui_State, varargin{:}); 

Error in ==> @(hObject,eventdata)init('popup_test_CreateFcn',hObject,eventdata,guidata(hObject)) 


??? Error using ==> struct2handle 
Error while evaluating uicontrol CreateFcn 

所以,出於某種原因,set()方法不是讓我來填充TestList彈出菜單。

有什麼想法?

在此先感謝。

回答

1

注意,當你運行你的程序,稱爲第一功能是"create functions"

所以,當你做set(handles.popup_test,'string',TestList);popup_test_CreateFcn,該函數不知道什麼是handles因爲它只有"opening function"後知道的。 (如果您嘗試在"create functions"內打印它,它將爲空)。

可以將此功能像這裏面的事:

handles.popup_test=hObject; %pass handles the popup menu object 
guidata(hObject, handles); 

而在開啓功能XXXX_OpeningFcn(hObject, eventdata, handles, varargin)您可以添加:

%...define TestList and other things you need 
set(handles.popup_test,'string',TestList); 
+0

非常感謝,我被現在擺脫了錯誤的最小。事情是當我現在運行它時,彈出菜單是空的。我有這樣的感覺,它與迭代填充TestList的位置有關,我是否正確地將它移動到'XXXX_OpeningFcn(hObject,eventdata,handles,varargin)'? –

+0

嘗試'set(handles.popup_test,'string',['a';'b';'c']);'如果它有效,因此如果您在'popup菜單'中看到'abc',那麼你知道你的文件存在問題。 – Maroun

+0

沒關係,我的壞。我解決了它,非常感謝! :) –