0

I'm trying to populate a popup menu from a GUI I made up with GUIDE. I'm doing as follows:

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);

I'm doing this inside the popup_test_CreateFcn method (I'm not really sure that is the right place though).

When trying to launch the GUI I keep getting this:

??? 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

So for some reason the set() method is not allowing me to populate the popup menu with TestList.

Any thoughts?

Thanks in advance.

Heartcroft
  • 1,672
  • 2
  • 19
  • 31

1 Answers1

1

Note that when you run your program, the first functions called are the "create functions"

So when you do set(handles.popup_test,'string',TestList); inside popup_test_CreateFcn, the function doesn't know what is handles because it is known only after the "opening function". (If you try to print it inside the "create functions" it will be empty).

You can do inside this function something like this:

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

And in the opening function XXXX_OpeningFcn(hObject, eventdata, handles, varargin) you can add:

%...define TestList and other things you need
set(handles.popup_test,'string',TestList);
Maroun
  • 94,125
  • 30
  • 188
  • 241
  • Thanks a lot, I got rid of the errors by now at least. The thing is the popup menu is empty when I run it now. I have the feeling it has to do with where iteration to fill the TestList is placed, did I do right moving that to `XXXX_OpeningFcn(hObject, eventdata, handles, varargin)`? – Heartcroft Dec 14 '12 at 13:58
  • Try to `set(handles.popup_test,'string',['a';'b';'c']);` and if it worked, hence if you can see `a b c` in your `popup menu`, then you know that the problem with your file. – Maroun Dec 14 '12 at 14:01
  • Plus you can print the content of your file to see weather it contains the desired values. – Maroun Dec 14 '12 at 14:01
  • Hey, sorry to bother you again. Any idea on why populating the popup menu like that I get `??? Cell contents reference from a non-cell array object.` When `str = get(handles.popup_entrenament, 'String'); val = get(handles.popup_entrenament, 'Value'); file = str{val};` to retrieve selected item? – Heartcroft Dec 14 '12 at 17:27
  • Nevermind again, I just had to use `mat2cell` so I could actually access a cell instead of a matrix :D – Heartcroft Dec 14 '12 at 18:53