Gui Matlab中的切换按钮

塔雷克

我想创建一个用作开/关按钮的按钮:如果用户按下该按钮,它将开始计数并在静态文本上显示计数器。如果用户再次按下它,它将停止计数。然后,如果用户第三次按下它,它将继续计数。

我尝试了这段代码

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

但是它不能正常工作:当我第一次按下按钮时,它开始计数,而当我再次按下按钮时,它的名称改变了,但仍在计数

the_raffa

在您当前的计数器实现中,首次按下按钮进行计数while,将startStop_togglebutton激活回调中循环Start

它会一直运行,直到条件(计数器<10)成立为止,即使您再次按下Stop计数按钮也是如此

因此,要解决这个问题,你可以使用value的的startStop_togglebuttoninsted的的“ 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

更新评论

之所以使用valuetogglebutton递增计数器如下。

首次按下来togglebutton启动计数器时,while无论循环是否完成循环都会被激活,并且GUI正在“等待”另一个回调。

此外,while循环已被编码到if捕获Start / Continue操作块中。

这意味着当您按下切换按钮停止计数器时,将跳过while循环。

通过在while循环中获取切换按钮的值,可以独立于预期的动作(停止/重新启动)捕获按钮状态的变化。

确实,当您将其按入“停止”计数器时,将value设置为,0因此当您按其重新启动时,它不会递增计数器,其值将设置为1并且计数器会递增。

我建议以其他方式对计数器GUI进行建模:

  • 将计数器编码在单独的“ .m”文件(或函数)中
  • 要在此“ .m”文件中编写计数器增量的逻辑(基于value切换按钮的)
  • 添加一个用于运行计数器的按钮(“ .m”文件)
  • startStop_togglebutton使用callbach来更新string切换按钮上的显示

通过使用该guidata函数,可以在GUI和“ .m”文件之间处理数据

他们”。文件通过其GUI来标识GUI tag(在我用来测试解决方案的代码中,我将GUI图形设置tagcounter_gui)。

您还应该HandleVisibility将GUI的属性设置为“ on”。

在下面您可以找到:

  • 更新的startStop_togglebutton回调,现在非常简单
  • 用于启动计数器的按钮的回调
  • “ .m”文件chitch管理计数器

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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

初学者-GUI切换按钮

来自分类Dev

Python GUI切换按钮试图切换标签图像

来自分类Dev

在Ubuntu中的控制台和GUI之间切换

来自分类Dev

在Ubuntu中的控制台和GUI之间切换

来自分类Dev

如何在17.10及更高版本中的控制台模式和GUI之间切换?

来自分类Dev

如何在RHEL8.1中在GUI自动登录和CLI登录之间切换

来自分类Dev

Ubuntu 16,04 GUI 仅在切换到 tty 或从 tty 切换时更新

来自分类Dev

在托管的C ++ GUI中结束/中断/一个事件被另一个事件切换

来自分类Dev

按钮组中的切换类

来自分类Dev

导航抽屉中的切换按钮

来自分类Dev

切换UITableViewCell中的单选按钮

来自分类Dev

WPF中的切换按钮

来自分类Dev

WPF中的切换按钮

来自分类Dev

反应中的切换按钮 onclick

来自分类Dev

在 jquery 中单击切换按钮

来自分类Dev

jQuery 中的按钮类切换

来自分类Dev

在MATLAB GUI中循环

来自分类Dev

在Matlab的GUI中绘图

来自分类Dev

NetBeans GUI设计器窗口在设计和源代码之间切换不显示

来自分类Dev

从本机iOS GUI切换到Qt GUI

来自分类Dev

在 phpMyAdmin 4.x 中,如何在不单击 FT GUI 切换的情况下将浏览恢复为部分而不是全文?

来自分类Dev

如何临时切换到GUI线程

来自分类Dev

删除/忘记切换按钮中的按钮

来自分类Dev

单选按钮样式作为 xaml 中的切换按钮

来自分类Dev

Matlab中的多页Gui

来自分类Dev

在Matlab GUI中拖放文件

来自分类Dev

Matlab中的多页Gui

来自分类Dev

处理Matlab GUI中的行为

来自分类Dev

jQuery切换按钮