2014-09-02 83 views
0

我有一個matlab Gui程序,它從串口獲取輸入數據並將它們繪製在圖形中。 Gui有幾個選項卡。在第二個選項卡中,我有一個popmenu,它允許我選擇要繪製的數據。Matlab Gui更新陰謀與popmenu

回調函數

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 

當我選擇在從串行字符串被分析popmenu第二個選項,數據被存儲在變量,然後繪圖。精細。

問題:只有當我點擊在popmenu第二個選項

數據被繪製。我怎樣才能獲得「實時」繪製的數據?

回答

0

好吧,我用三個計時器在0.1秒

申報定時器恆定速率首先執行的情節任務。

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 

完成啓動合適的定時器!

0

關鍵是你想要使用的觸發事件。如果您希望在選擇第二個選項卡時自動更新它,則應該採取「選擇第二個選項卡」的操作作爲觸發事件以更新圖。