Maryam
Maryam

Reputation: 35

How to add new variable in popup menu in Gui?

I have 2 popup menus (in GUI). With the first one user can choose a operation and with the second one user should be capable to select a name for the file that would be saved during the selected operation.

In the other words, I have to define two options in the second popup menu:

  1. Standard(it means the file will be saved by the default name which is defined in the program)

  2. ... (user can enter the new name)

Is it possible?

Upvotes: 0

Views: 1315

Answers (1)

il_raffa
il_raffa

Reputation: 5190

I've built a simple GUI consisting of just two popupmenu with tag: popupmenu1 and popupmenu2.

popupmenu1 just contains some string, just to test the code.

popupmenu2 contains two strings: Save to default file and Select a new file

enter image description here

To solve your problem, you can add the following lines of code in the second popupmenu callback.

The proposed solution works this way:

if he user selects the fist option, the output will be saved in the default output file. Notice, you should add the code to actually save the output in the default output file

if the user selects the second option, the uiputfile GUI ask the user to define / select the output file. Some checks heve beem inserted on the file selction. Also in this case, you should add the code to actually save the output in the default output file

% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu2 contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu2

% Identify the first popupm menu selected option
% not strictly necessary, just used to generare the messsage box text
sel_op=get(handles.popupmenu1,'value');
% Idetify the selected option in the second popupmenu
opt=get(hObject,'value')
% Test the second popup menu selection:
%    if opt == 1: the default output file has been selected
if(opt == 1)
   %
   % Insert here the code to save the output in the default output file
   %
   msgbox(['Results of Operation #' num2str(sel_op) ' will be saved in the default output file'], ...
           'Output file selection')
else
% if the second optino has been selected, the user is prompt to select the
% output file
   [filename, pathname] = uiputfile( ...
      {'*.m';'*.mdl';'*.mat';'*.*'}, ...
      'Save as');
% Check for the file selection
   if(filename == 0)
      msgbox('Output file selction aborted','Output file selection')
   else
      %
      % Insert here the code to save the output in the user-defined 
      % output file
      %
      output_file_name=fullfile(pathname,filename)
      msgbox(['Results of Operation #' num2str(sel_op) 'will be saved in ' output_file_name], ...
         'Output file selection')
   end
end

Hope this helps.

Upvotes: 1

Related Questions