-3

弹出菜单起初似乎是空的,在关闭并重新打开菜单后,变量会显示出来。我该如何更改它以在第一次打开时显示它。这是代码:

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

目标寻找弹出窗口

4

1 回答 1

1

假设您将菜单定义为Pop=uicontrol('style','popupmenu','string',' ');

然后在代码中Pop可以访问的任何地方,您可以使用以下行之一:

%// assign only 2 number to the Pop menu
set(Pop, 'string', {num2str(val1); num2str(val2)});

%// assign only 2 numbers with labels to the Pop menu     
set(Pop, 'string', {...
        ['Val1 = ' num2str(val1)]; ...
        ['Val2 = ' num2str(val2)]});

或者,您可以通过以下方式使其更具可读性和灵活性:

Pop_string = get(Pop, 'string');     %// Read actual String in Pop
Pop_string{3} = ['Val3 = ', Val3];   %// Update 3rd element
set(Pop, 'String', Pop_string);      %// Update the String in Pop

编辑:

我使用匿名函数制作了一个示例代码,请在此处查看详细信息:从 GUI 访问嵌套函数

function[]=activePop()
close all,clc
fig=figure;
Pop=uicontrol('style','popupmenu','string',' ');
uicontrol('style','pushbutton','string','Reset',...
  'callback',@(s,a)PushReset(),'position',[5 1 1 1].*get(Pop,'position'));
uicontrol('style','pushbutton','string','Update',...
  'callback',@(s,a)PushUpdate(),'position',[10 1 1 1].*get(Pop,'position'));

  function PushReset()      %// Resets the Pop's list
    N=ceil(5*rand(1));    %// The menu will have 1 to 5 entries
    Labels=cell(N,1);

    for ii=1:N
        Labels{ii}=['Val' num2str(ii) ' = ' num2str(rand)];  %// assign 'Val(ii) = ' label and random value to the list
    end

    set(Pop,'string',Labels)   %// display the list in Pop's menu
  end

  function PushUpdate()   %// Change one (randomly selected) value in Pop's menu
    PopString=get(Pop,'string');   %// get actual List of entries
    N=size(PopString,1);           %// find it's size
    ii=ceil(N*rand);               %// pick one random element
    Line=PopString{ii};            %// read the chosen line
    Line=regexp(Line,' ','split'); %// extract the label
    Line=[Line{1},' ',Line{2},' ',num2str(rand)]; %// update the line
    PopString{ii}=Line;            %// update the line
    set(Pop,'string',PopString);   %// send updated list to the Pop menu
  end

end
于 2016-03-14T14:55:28.073 回答