我想创建一个按钮作为开/关开关:如果用户按下它,它会开始计数并在静态文本上显示计数器。如果用户再次按下它,它将停止计数。然后,如果用户第三次按下它,它将继续计数。
我试过这段代码
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
但是它没有正常工作:当我第一次按下按钮时,它开始计数,当我按下它一秒钟时,它的名字改变了,但它还在计算中
答案 0 :(得分:1)
在您当前的计数器实施中,while
回调中的startStop_togglebutton
循环会在您第一次按下按钮进行Start
计数时激活。
即使您再次按下ushbutton到Stop
计数,它仍会一直运行,直到条件(计数器<10)成立。
因此,要解决此问题,您可以使用“value
”的startStop_togglebutton
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
的{{1}}递增计数器的原因如下:
第一次按togglebutton
启动计数器时,togglebutton
循环被激活,GUI正在“等待”另一个回调,无论循环是否完成。
此外,while循环已编码到捕捉开始/继续操作的while
块中。
这意味着当你按下togglebutton来停止计数器时,会跳过while循环。
在while循环中获取togglebutton的值可以独立于预期的操作(停止/重新启动)捕获按钮状态的变化。
实际上,当您将其推送到停止计数器时,if
设置为value
,因此当您将其推送以重新启动时,它不会递增计数器,其值将设置为{{ 1}}并且计数器递增。
我建议以不同的方式对计数器GUI进行建模:
0
)1
callbach更新togglebutton上显示的value
使用startStop_togglebutton
函数处理GUI和“.m”文件之间的数据。
“。m”。文件通过string
识别GUI(在我用于测试解决方案的代码中,我将GUI图guidata
设置为tag
)。
您还应该将GUI的属性tag
设置为“on”
以下您可以找到:
startStop_togglebutton回调
counter_gui
run_counter回调
HandleVisibility
“。m”管理计数器的文件
% --- 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
希望这有帮助。