2015-01-09 3 views
0

저는 MATLAB GUI로 반응 테스트를 프로그래밍하려고합니다.Matlab GUI 버튼 반응

모든 클릭 후 임의의 위치에 하나의 버튼이 표시되고 나면 다음 클릭에 필요한 시간이 표시됩니다.

이제이 버튼을 10 번 클릭하면 사라지 길 원합니다.

어떻게하면 10 번째 클릭 후 "닫기 all"을 프로그래밍 할 수 있습니까?

+0

주요 문제는 어딘가에 카운터를 유지해야하지만 GUI 콜백 기능에는 지속적인 변수가 없다는 것입니다. 자문 해보십시오 : GUI 콜백 함수 호출간에 액세스 할 수있는 곳에서 어디에서 '카운터'변수를 유지할 수 있습니까? –

답변

0

다음은 원하는 것을 수행하는 코드입니다.

@ Li-aung이 언급했듯이, 푸시 버튼을 누른 횟수를 추적하는 카운터를 추가했습니다. 카운터는 그림의 핸들 구조에 저장됩니다. 그러나이를 단순화하기 위해 전역 변수로 만들 수 있습니다.

enter image description here

희망 시작하는 데 도움이 :

여기
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 

은 GUI의 스크린 샷입니다 :

여기에 주석이있는 코드입니다!

+0

그래서 @freaky 내 제안을 시도 했습니까? –