2010-05-10 49 views
0

我想創建一個動畫,使用戶能夠通過模擬的步驟前進和後退。Matlab GUI:如何保存函數的結果(應用程序的狀態)

動畫必須模擬信道解碼的迭代過程(接收器接收一個比特塊,執行一個操作,然後檢查該塊是否對應於奇偶校驗規則,如果塊不對應,則再次執行該操作並且該過程最終在代碼對應於給定規則時結束)。

我寫了執行解碼過程的函數並返回一個m x n x i矩陣,其中m x n是數據塊,i是迭代索引。因此,如果需要3次迭代才能解碼數據,則該函數會返回一個m x n x 3矩陣,每個步驟都被攪動。

在GUI(.fig文件)中,我放置了一個運行解碼方法的「解碼」按鈕,並且有按鈕「後退」和「前進」,它們必須使用戶能夠在記錄的步驟。

我已將「decodedData」矩陣和currentStep值存儲爲全局變量,因此通過單擊「前進」和「下一個」按鈕,索引必須更改並指向適當的步驟狀態。

當我試圖調試應用程序時,該方法返回瞭解碼數據,但是當我嘗試單擊「後退」和「下一步」時,解碼後的數據似乎沒有被聲明。

是否有人知道如何訪問(或存儲)函數的結果以便啓用我想在Matlab GUI中實現的描述邏輯?

回答

1

訣竅是,使得它們共享相同的工作區使用嵌套函數。因爲我已經有example in your last question開始,現在我簡單地添加GUI控件來實現前進/後退交互,除了播放/停止動畫:

function testAnimationGUI() 
    %# coordinates 
    t = (0:.01:2*pi)';   %# 'fix SO syntax highlight 
    D = [cos(t) -sin(t)]; 

    %# setup a figure and axis 
    hFig = figure('Backingstore','off', 'DoubleBuffer','on'); 
    hAx = axes('Parent',hFig, 'XLim',[-1 1], 'YLim',[-1 1], ... 
       'Drawmode','fast', 'NextPlot','add'); 
    axis(hAx, 'off','square') 

    %# draw circular path 
    line(D(:,1), D(:,2), 'Color',[.3 .3 .3], 'LineWidth',1); 

    %# initialize point 
    hLine = line('XData',D(1,1), 'YData',D(1,2), 'EraseMode','xor', ... 
       'Color','r', 'marker','.', 'MarkerSize',50); 
    %# init text 
    hTxt = text(0, 0, num2str(t(1)), 'FontSize',12, 'EraseMode','xor'); 

    i=0; 
    animation = false; 

    hBeginButton = uicontrol('Parent',hFig, 'Position',[1 1 30 20], ... 
          'String','<<', 'Callback',@beginButton_callback); 
    hPrevButton = uicontrol('Parent',hFig, 'Position',[30 1 30 20], ... 
          'String','<', 'Callback',@previousButton_callback); 
    hNextButton = uicontrol('Parent',hFig, 'Position',[60 1 30 20], ... 
          'String','>', 'Callback',@nextButton_callback); 
    hEndButton = uicontrol('Parent',hFig, 'Position',[90 1 30 20], ... 
          'String','>>', 'Callback',@endButton_callback); 

    hSlider = uicontrol('Parent',hFig, 'Style','slider', 'Value',1, 'Min',1,... 
         'Max',numel(t), 'SliderStep', [10 100]./numel(t), ... 
         'Position',[150 1 300 20], 'Callback',@slider_callback); 

    hPlayButton = uicontrol('Parent',hFig, 'Position',[500 1 30 20], ... 
          'String','|>', 'Callback',@playButton_callback); 
    hStopButton = uicontrol('Parent',hFig, 'Position',[530 1 30 20], ... 
          'String','#', 'Callback',@stopButton_callback); 

    %#----------- NESTED CALLBACK FUNCTIONS ----------------- 
    function beginButton_callback(hObj,eventdata) 
     updateCircle(1) 
    end 

    function endButton_callback(hObj,eventdata) 
     updateCircle(numel(t)) 
    end 
    function nextButton_callback(hObj,eventdata) 
     i = i+1; 
     if (i > numel(t)), i = 1; end 
     updateCircle(i) 
    end 

    function previousButton_callback(hObj,eventdata) 
     i = i-1; 
     if (i < 1), i = numel(t); end 
     updateCircle(i) 
    end 

    function slider_callback(hObj, eventdata) 
     i = round(get(gcbo,'Value')); 
     updateCircle(i) 
    end 

    function playButton_callback(hObj, eventdata) 
     animation = true; 
     while animation 
      i = i+1; 
      if (i > numel(t)), i = 1; end 
      updateCircle(i) 
     end 
    end 

    function stopButton_callback(hObj, eventdata) 
     animation = false; 
    end 

    function updateCircle(idx) 
     set(hSlider, 'Value', rem(idx-1,numel(t))+1) %# update slider to match 

     set(hLine,'XData',D(idx,1), 'YData',D(idx,2)) %# update X/Y data 
     set(hTxt,'String',num2str(t(idx)))   %# update angle text 
     drawnow          %# force refresh 
     if ~ishandle(hAx), return; end    %# check valid handle 
    end 
    %#------------------------------------------------------- 
end 

您可能會發現滑塊功能有點馬車,但你明白了:)