我想在Matlab的弹出菜单中打印出一些变量。弹出菜单首先显示为空,关闭并重新打开菜单后,变量显示出来。如何更改它以在第一个开头显示它。
答案 0 :(得分: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
修改强>
我使用匿名函数制作了示例代码,详见此处:Access nested functions from 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