Reputation: 95968
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:
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?
Upvotes: 4
Views: 10690
Reputation: 3037
Background
A few things that may be helpful:
gcbf
returns the current callback figure.union
can return the union of cell arrays of strings.Suggestions
Here is how I might write such a function. This puts everything in one place, the function can be called from anywhere within your GUI, it automatically updates the cached list, and eliminates any duplicate entries.
You would call this code at the end of your init code, as well as the callback for adding folders. The Tag can be set by right-clicking and setting properties within GUIDE.
updatePopupMenu()
popupMenuHandle = findobj(gcbf,'Tag','myPopupMenuTag');
popupMenuContents = get(popupMenuHandle,'String');
% Initialization
if isempty(popupMenuContests)
fid_r = fopen('targets.txt', 'r');
C = textscan(fid_r, '%s');
popupMenuContents = C{1};
end
% Join
otherFields = howeverYouGetFieldsFromOtherList();
combinedContents = union(popupMenuContents, otherFields);
% Save
set(popupMenuHandle,'String', combinedContents);
fid_w = fopen('targets.txt','w+');
for i = 1:length(combinedContents)
fprintf(fid_w,'%s\n',combinedContents{i});
end
fclose(fid_w);
end
Upvotes: 2
Reputation: 21563
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.
Upvotes: 2