Gui Matlab中的切换按钮
toggle button in Gui Matlab
我想创建一个用作 on/off 开关的按钮:如果用户按下它,它就会开始计数并在静态文本上显示计数器。如果用户再次按下它,它就会停止计数。然后,如果用户第三次按下它,它会继续计数。
我试过这个代码
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
但它不能正常工作:当我第一次按下按钮时,它开始计数,当我按下它一秒钟时,它的名称发生了变化但它仍在计数.
在您当前的计数器实现中,startStop_togglebutton
回调中的 while
循环在您第一次按下按钮进行 Start
计数时被激活。
它保持 运行ning 直到条件(计数器 < 10)成立,即使您再次按下 ushbutton 以 Stop
计数。
因此,要解决此问题,您可以使用“1
”的 startStop_togglebutton
的 value
来增加计数器。
您可以在下面找到回调的更新版本。我还添加了几个“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
更新以回答评论
使用 togglebutton
的 value
来递增计数器的原因如下。
第一次按 togglebutton
启动计数器时,while
循环被激活,GUI 是 "waiting" 用于另一个回调,无论循环是否完成。
此外,while 循环已被编码到捕获开始/继续操作的 if
块中。
这意味着当您按下切换按钮停止计数器时,将跳过 while 循环。
在 while 循环中获取切换按钮的值允许独立于预期操作(停止/重新启动)捕获按钮状态的变化。
确实,当您按下它以停止计数器时,value
设置为 0
因此它不会增加计数器,而当您按下它以重新启动时,其值设置为1
并且计数器递增。
我建议以不同的方式对计数器 GUI 进行建模:
- 在单独的“.m”文件(或函数)中对计数器进行编码
- 在此“.m”文件中编写计数器增量的逻辑(基于切换按钮的
value
)
- 添加一个按钮,运行 计数器(“.m”文件)
- 使用
startStop_togglebutton
callbach 仅更新切换按钮上显示的 string
GUI 和“.m”文件之间的数据处理是通过使用 guidata
函数完成的。
“.m”。 files 通过其 tag
标识 GUI(在我用来测试解决方案的代码中,我将 GUI 图 tag
设置为 counter_gui
)。
您还应该将 GUI 的 属性 HandleVisibility
设置为 'on'
您可以在下面找到:
- 更新后的 startStop_togglebutton 回调,现在非常简单
- 用于启动计数器的按钮的回调
- “.m”文件 chich 管理计数器
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');
希望这对您有所帮助。
我想创建一个用作 on/off 开关的按钮:如果用户按下它,它就会开始计数并在静态文本上显示计数器。如果用户再次按下它,它就会停止计数。然后,如果用户第三次按下它,它会继续计数。
我试过这个代码
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
但它不能正常工作:当我第一次按下按钮时,它开始计数,当我按下它一秒钟时,它的名称发生了变化但它仍在计数.
在您当前的计数器实现中,startStop_togglebutton
回调中的 while
循环在您第一次按下按钮进行 Start
计数时被激活。
它保持 运行ning 直到条件(计数器 < 10)成立,即使您再次按下 ushbutton 以 Stop
计数。
因此,要解决此问题,您可以使用“1
”的 startStop_togglebutton
的 value
来增加计数器。
您可以在下面找到回调的更新版本。我还添加了几个“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
更新以回答评论
使用 togglebutton
的 value
来递增计数器的原因如下。
第一次按 togglebutton
启动计数器时,while
循环被激活,GUI 是 "waiting" 用于另一个回调,无论循环是否完成。
此外,while 循环已被编码到捕获开始/继续操作的 if
块中。
这意味着当您按下切换按钮停止计数器时,将跳过 while 循环。
在 while 循环中获取切换按钮的值允许独立于预期操作(停止/重新启动)捕获按钮状态的变化。
确实,当您按下它以停止计数器时,value
设置为 0
因此它不会增加计数器,而当您按下它以重新启动时,其值设置为1
并且计数器递增。
我建议以不同的方式对计数器 GUI 进行建模:
- 在单独的“.m”文件(或函数)中对计数器进行编码
- 在此“.m”文件中编写计数器增量的逻辑(基于切换按钮的
value
) - 添加一个按钮,运行 计数器(“.m”文件)
- 使用
startStop_togglebutton
callbach 仅更新切换按钮上显示的string
GUI 和“.m”文件之间的数据处理是通过使用 guidata
函数完成的。
“.m”。 files 通过其 tag
标识 GUI(在我用来测试解决方案的代码中,我将 GUI 图 tag
设置为 counter_gui
)。
您还应该将 GUI 的 属性 HandleVisibility
设置为 'on'
您可以在下面找到:
- 更新后的 startStop_togglebutton 回调,现在非常简单
- 用于启动计数器的按钮的回调
- “.m”文件 chich 管理计数器
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');
希望这对您有所帮助。