【问题标题】:Matlab Gui update plot with popmenuMatlab Gui 使用 popmenu 更新绘图
【发布时间】:2014-09-03 07:56:49
【问题描述】:

我有一个 matlab Gui 程序,它从串行中获取输入数据并将它们绘制在图表中。 Gui 有几个选项卡。在第二个选项卡中,我有一个弹出菜单,允许我选择要绘制的数据。

回调函数

function popupCallback(src,~)
    val = get(src,'Value');
    % Second tab selected
    if val == 2
        try
            while (get(xbee, 'BytesAvailable')~=0 && tenzo == true)
                % reads until terminator
                sentence = fscanf( xbee, '%s');

                % Collect data to plot
                getDataRoutine(sentence)

                %Plot them            
                h1 = subplot(3,1,1,'Parent',hTabs(3));
                plot(h1,index,gxdata,'r','LineWidth',2);
                h2 = subplot(3,1,2,'Parent',hTabs(3));
                plot(h2,index,gydata,'b','LineWidth',2);
                h3 = subplot(3,1,3,'Parent',hTabs(3));
                plot(h3,index,gzdata,'g','LineWidth',2);
            end
        end
    end

当我在弹出菜单中选择第二个选项时,分析来自串行的字符串,数据存储在变量中,然后绘制。很好。

问题:

只有当我点击弹出菜单中的第二个选项时才会绘制数据。如何获取“实时”绘制的数据?

【问题讨论】:

    标签: matlab matlab-guide


    【解决方案1】:

    关键是你要使用什么触发事件。如果您希望它在您选择第二个选项卡时自动更新,您应该将“选择第二个选项卡”作为触发事件来更新绘图。

    【讨论】:

      【解决方案2】:

      好的,我用了三个定时器以0.1s的恒定速率执行绘图任务

      先声明计时器。

      global gyroTimer;
      gyroTimer = timer('ExecutionMode','FixedRate','Period',0.1,'TimerFcn',{@graphGyro});
      
      global angleTimer;
      angleTimer = timer('ExecutionMode','FixedRate','Period',0.1,'TimerFcn',{@graphAngles});
      
      global accTimer;
      accTimer = timer('ExecutionMode','FixedRate','Period',0.1,'TimerFcn',{@graphAcc});
      

      在Popmenu回调函数中选择相应的选项

      时启动正确的计时器
      % drop-down menu callback
      function popupCallback(src,~)
          %# update plot color
          val = get(src,'Value');
      
          % Gyro
          if val == 2            
              start(gyroTimer);
              stop(angleTimer);
              stop(accTimer);
          end
      
          % Accelerometer
          if val == 3
              stop(gyroTimer);
              stop(angleTimer);
              start(accTimer);                    
          end
      
          % Magnetometer
          if val == 4
             stop(gyroTimer);
             start(angleTimer);
             stop(accTimer); 
          end
      end
      

      创建绘图函数

      function graphAngles(obj,event,handles)
          % To debug uncomment the following line
          %disp('Angles');
      
          h1 = subplot(3,1,1,'Parent',hTabs(3));
          plot(h1,index,Tdata,'r','LineWidth',2);
          hold on;
          plot(h1,index,EKXdata,'b-','LineWidth',1);
          hold off;
      
          h2 = subplot(3,1,2,'Parent',hTabs(3));
          plot(h2,index,Pdata,'r','LineWidth',2);
          hold on;
          plot(h2,index,EKYdata,'b-','LineWidth',1);
          hold off;
      
          h3 = subplot(3,1,3,'Parent',hTabs(3));
          plot(h3,index,Ydata,'r','LineWidth',2);
      end
      

      完成!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-16
        • 1970-01-01
        • 2016-12-31
        • 2015-08-27
        相关资源
        最近更新 更多