这里有一些你想要的代码。
正如@Li-aung 提到的,我添加了一个计数器,用于跟踪按钮被按下的次数。计数器存储在图的handles结构中;但是,为简单起见,您可以将其设为全局变量。
这是带有 cmets 的代码:
function RandomButton(~)
hFig = figure('Position',[100 100 200 200],'Visible','off');
handles.CounterText = uicontrol('Style','text','Position',[50 150 60 30],'String','Counter')
handles.DisplayCounterText = uicontrol('Style','text','Position',[50 100 60 30],'String','0')
handles.Button = uicontrol('Style','pushbutton','position',[50 50 60 30],'String','Push here','Callback',@(s,e) Push);
handles.PushCounter = 0; %// Initialize counter
movegui(gcf,'center')
set(hFig,'Visible','on')
guidata(hFig,handles)
function Push
handles = guidata(hFig);
handles.PushCounter = handles.PushCounter +1;
set(handles.DisplayCounterText,'String',num2str(handles.PushCounter));
if handles.PushCounter < 10 %// Assign condition to stop.
set(hFig,'Visible','off');
set(hFig,'Position',[1000*rand(1) 1000*rand(1) 200 200]); %// Assign random position
set(hFig,'Visible','on');
guidata(hFig,handles) %// Update handles structure. Important!
else
close all
end
end
end
这是图形用户界面的截图:
希望能帮助您入门!