Matlab GUI: Dynamically changing the popup menu

Maroun picture Maroun · Dec 14, 2012 · Viewed 10.4k times · Source

I have a GUI that I want to add a popup menu to it. The popup menu fields that should be shown are saved in the file targets.txt.

When I open my program, I want the popup menu to include the lines from the mentioned file above. I'm doing this because I want the popup menu to change dynamically in the program. Since it will include directories paths that the user entered in another field, I'm saving the directories paths in a file, and each time a user enters a folder, I set the popup menu according to the file. (I did it and it works fine)

Since function myFunction_OpeningFcn(hObject, eventdata, handles, varargin) is called only after calling the "create function" of each component of the GUI, I couldn't do the initialization in the "opening function" of the program. Instead, I had to to something like this:

function databaseMenu_CreateFcn(hObject, eventdata, handles)
if ispc&&isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
handles.databaseMenuObject=hObject; % (1) see below
guidata(hObject, handles);

(1): I save the popup menu object in handles so that I can use it in the opening function.

And then, in the opening function I can do:

fid_r = fopen('targets.txt', 'r');
C = textscan(fid_r, '%s');
set(handles.databaseMenuObject,'String', C{1});

So, when someone adds a new "database" folder in the program, the popup menu changes (I add the folder that was chosen by the user to the file, and then I set the popup menu to have its field from the file (the function above). So it'll look like that:

The program after the user entered two folders

I don't like the design of my code, and I couldn't figure out how to do it in a different way, is there a way to make the "create function" of the "popup menu" to be called after the "opening function" of the program? Or is there a better way to achieve my goal?

Answer

Dennis Jaheruddin picture Dennis Jaheruddin · Dec 14, 2012

Alright I now understand what you want to achieve, though I am not sure whether I understand the problem I hope this helps:

Judging from the description this seems like a logical order for things to happen:

1: Initalization, just initialize everything, you already know that you will have a dropdown menu but you just don't know the content yet. Therefore just start with a defaultoption or empty (possibly invisible).

2: Update, As soon as the users saves new input, you update the list.