【问题标题】:Making MATLAB GUI Radio button as figure visibility ON-OFF制作 MATLAB GUI 单选按钮作为图形可见性 ON-OFF
【发布时间】:2016-07-19 18:13:40
【问题描述】:

我正在为我的一个脚本开发 MATLAB GUI。我的脚本有“开始”按钮,我在该按钮下调用函数来求解方程。我有3个数字。所以到目前为止,我的 GUI 运行良好。发生的情况是,当用户在提供所需输入后单击“开始”时,3 个数字会一一弹出,并且我已经指定了保存数字的位置。循环很小的时候看起来不错,但是当循环很大时,计算时间会增加,所以弹出的数字会让用户感到恼火,因为用户无法做任何事情。

我想在 GUI 中引入一个单选按钮,取消选中该按钮将禁用图形的可见性。 我尝试了 figure('Visible','off') 并且效果很好,但现在我希望它与单选按钮链接。

单选按钮回调是:

% --- Executes on button press in checkbox2.
function checkbox2_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox2 (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 checkbox2

谁能帮我确定代码的框架,以使此单选按钮打开-关闭按钮中数字的可见性:

function Start_Callback(hObject, eventdata, handles)
% hObject    handle to Start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

等待您的帮助。

【问题讨论】:

    标签: matlab


    【解决方案1】:

    在您的Start_Callback 中检查复选框/单选按钮的状态并创建一个新变量以供以后使用:

    if get(handles.checkbox2, 'Value')
        Visibility = 'on';
    else 
        Visibility = 'off';
    end
    

    现在创建你的人物:

    figure('Visible', Visibility)
    

    取决于Visibility 的值,它们是否可见

    【讨论】:

      【解决方案2】:

      我认为您的主要问题是设置所有三个数字的可见性。
      要设置特定图形的可见性,最好的选择是保持对该图形的处理。

      我建议打开新图时保留图柄:

      h1 = figure; %Open new figure, and store figure handle in h1
      h2 = figure; %Open new figure, and store figure handle in h2
      h3 = figure; %Open new figure, and store figure handle in h3

      我认为在 GUI OpeningFcg 函数中创建(打开)图形的正确位置。

      创建新GUI时,使用引导工具,Matlab生成如下代码:

      function guiname_OpeningFcg(hObject, evevntdata, handles, varargin)  
      ...
      handles.output = hObject;
      
      guidata(hObject, handles);
      ...
      

      在代码行guidata(hObject, handles);之后,您可以创建您的数字。
      将图形句柄存储在handles 结构中以供以后使用:

      function guiname_OpeningFcg(hObject, evevntdata, handles, varargin) 
      ...
      handles.output = hObject;
      
      guidata(hObject, handles);
      
      h1 = figure; %Open first figure, and store figure handle in h1
      h2 = figure; %Open second figure, and store figure handle in h2
      h3 = figure; %Open third figure, and store figure handle in h3
      
      handles.h1 = h1; %Store handle to first figure.
      handles.h2 = h2; %Store handle to second figure.
      handles.h2 = h2; %Store handle to third figure.
      

      在回调函数中,使用如下代码设置可见性:

      % --- Executes on button press in checkbox2.
      function checkbox2_Callback(hObject, eventdata, handles)
      
      val = get(handles.checkbox2, 'Value');
      
      if (val)
          vis = 'on';
      else
          vis = 'off';
      end
      
      set(handles.h1, 'Visible', vis); %Set Visibility of first figure.
      set(handles.h2, 'Visible', vis); %Set Visibility of second figure.
      set(handles.h3, 'Visible', vis); %Set Visibility of third figure.
      

      【讨论】:

      • 谢谢。我也试过这个方法。它工作得很好,但@serial 的方法最简单。
      猜你喜欢
      • 2023-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-07
      • 1970-01-01
      • 2013-09-25
      相关资源
      最近更新 更多