如何在 Gui 的弹出菜单中添加新变量?

How to add new variable in popup menu in Gui?

我有 2 个弹出菜单(在 GUI 中)。第一个用户可以选择一个操作,第二个用户应该能够 select 为将在 selected 操作期间保存的文件命名。

换句话说,我必须在第二个弹出菜单中定义两个选项:

  1. 标准(即文件将以程序中定义的默认名称保存)

  2. ...(用户可以输入新名称)

可能吗?

我构建了一个简单的 GUI,它只包含两个 popupmenu 和标签:popupmenu1popupmenu2

popupmenu1 只是包含一些字符串,只是为了测试代码。

popupmenu2 包含两个字符串:Save to default fileSelect a new file

要解决您的问题,您可以在第二个弹出菜单中添加以下代码行callback

建议的解决方案是这样工作的:

如果用户select选择第一个选项,输出将保存在默认输出文件中。 注意,您应该添加代码以将输出实际保存在默认输出文件中

如果用户 select 选择第二个选项,uiputfile GUI 会要求用户定义/select 输出文件。 一些检查已插入到文件选择中。 同样在这种情况下,您应该添加代码以实际将输出保存在默认输出文件中

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

希望这对您有所帮助。