我想创建一个用作开/关按钮的按钮:如果用户按下该按钮,它将开始计数并在静态文本上显示计数器。如果用户再次按下它,它将停止计数。然后,如果用户第三次按下它,它将继续计数。
我尝试了这段代码
function startStop_togglebutton_Callback(hObject, eventdata, handles)
% hObject handle to startStop_togglebutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of startStop_togglebutton
persistent counter ;
if isempty(counter)
counter = 0 ;
end
button_state = get(hObject,'Value');
if button_state == get(hObject,'Max')
set(handles.startStop_togglebutton,'String','Stop','ForegroundColor','red')
setappdata(handles.startStop_togglebutton,'sw',1)
while counter < 10
counter = counter+1;
set(handles.statusBar,'String',strcat(['the counter = ' num2str(counter) ]))
pause(1)
end
set(handles.startStop_togglebutton,'String','Finished','ForegroundColor','cyan')
elseif button_state == get(hobject,'min')
set(handles.startstop_togglebutton,'string','resume','foregroundcolor','blue')
setappdata(handles.startstop_togglebutton,'sw',0)
set(handles.statusBar,'String',strcat([' stopped & the counter = ' num2str(counter) ' !']))
end
但是它不能正常工作:当我第一次按下按钮时,它开始计数,而当我再次按下按钮时,它的名称改变了,但仍在计数。
在您当前的计数器实现中,首次按下按钮进行计数时while
,将startStop_togglebutton
激活回调中的循环Start
。
它会一直运行,直到条件(计数器<10)成立为止,即使您再次按下Stop
计数按钮也是如此。
因此,要解决这个问题,你可以使用value
的的startStop_togglebutton
insted的的“ 1
”递增计数器。
在下面,您可以找到回调的更新版本。我还添加了几个“ if
”块来管理字符串在statusbar
% --- Executes on button press in startStop_togglebutton.
function startStop_togglebutton_Callback(hObject, eventdata, handles)
% hObject handle to startStop_togglebutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of startStop_togglebutton
% hObject handle to startStop_togglebutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of startStop_togglebutton
persistent counter ;
if isempty(counter)
counter = 0 ;
end
button_state = get(hObject,'Value');
if button_state == get(hObject,'Max')
set(handles.startStop_togglebutton,'String','Stop','ForegroundColor','red')
setappdata(handles.startStop_togglebutton,'sw',1)
while counter < 10
%
% Inserted acquisition of button state within the while loop
%
button_state = get(hObject,'Value');
%
% Modified the counter increment:
% the increment is based on the status of the button
%
% counter = counter+1;
counter = counter+button_state;
%
% Added "if" condition
% The "statusbar" is updated only if counting is on
%
if(button_state)
set(handles.statusBar,'String',strcat(['the counter = ' num2str(counter) ]))
end
pause(1)
end
%
% Added "if" condition
% The "statusbar" is updatred only if counting is finished
%
if(counter == 10)
set(handles.startStop_togglebutton,'String','Finished','ForegroundColor','cyan')
end
elseif button_state == get(hObject,'min')
set(handles.startStop_togglebutton,'string','resume','foregroundcolor','blue')
setappdata(handles.startStop_togglebutton,'sw',0)
set(handles.statusBar,'String',strcat([' stopped & the counter = ' num2str(counter) ' !']))
end
更新评论
之所以使用value
的togglebutton
递增计数器如下。
首次按下来togglebutton
启动计数器时,while
无论循环是否完成,循环都会被激活,并且GUI正在“等待”另一个回调。
此外,while循环已被编码到if
捕获Start / Continue操作的块中。
这意味着当您按下切换按钮停止计数器时,将跳过while循环。
通过在while循环中获取切换按钮的值,可以独立于预期的动作(停止/重新启动)捕获按钮状态的变化。
确实,当您将其按入“停止”计数器时,将value
设置为,0
因此当您按其重新启动时,它不会递增计数器,其值将设置为1
并且计数器会递增。
我建议以其他方式对计数器GUI进行建模:
value
切换按钮的)startStop_togglebutton
仅使用callbach来更新string
切换按钮上的显示通过使用该guidata
函数,可以在GUI和“ .m”文件之间处理数据。
他们”。文件通过其GUI来标识GUI tag
(在我用来测试解决方案的代码中,我将GUI图形设置tag
为counter_gui
)。
您还应该HandleVisibility
将GUI的属性设置为“ on”。
在下面您可以找到:
startStop_togglebutton回调
% --- Executes on button press in startStop_togglebutton.
function startStop_togglebutton_Callback(hObject, eventdata, handles)
% hObject handle to startStop_togglebutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of startStop_togglebutton
% hObject handle to startStop_togglebutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of startStop_togglebutton
button_state = get(hObject,'Value');
if(button_state == 1)
set(handles.startStop_togglebutton,'String','Stop','ForegroundColor','red')
else
set(handles.startStop_togglebutton,'string','resume','foregroundcolor','blue')
end
run_counter回调
% --- Executes on button press in run_counter.
function run_counter_Callback(hObject, eventdata, handles)
% hObject handle to run_counter (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.startStop_togglebutton,'enable','on');
set(handles.startStop_togglebutton,'Value',1);
set(handles.run_counter,'enable','off');
% Run the counter ".m" file
run_counter;
“ .m”文件管理计数器
% Get tha handle of the GUI figure
gui_h=findobj('tag','counter_gui');
% Get gudata
gui_my_data=guidata(gui_h);
counter=0;
while(counter < 10)
% Get togglebutton value
button_status=get(gui_my_data.startStop_togglebutton,'value');
% Increment to counter only if the togglebutton is set to "Start/Resume"
counter=counter+button_status;
% Update strings
if(button_status)
set(gui_my_data.startStop_togglebutton,'String','Stop','ForegroundColor','red')
set(gui_my_data.statusBar,'String',strcat(['the counter = ' num2str(counter) ]))
else
set(gui_my_data.statusBar,'String',strcat([' stopped & the counter = ' num2str(counter) ' !']))
end
pause(1);
end
set(gui_my_data.startStop_togglebutton,'String','Finished','ForegroundColor','cyan')
set(gui_my_data.startStop_togglebutton,'enable','off');
set(gui_my_data.run_counter,'enable','on');
希望这可以帮助。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句