开发者

Matlab GUI: How to Save the Results of Functions (states of application)

I would like to create an animation which enables the user to go backward and forward through the steps of simulation.

An animation has to simulate the iterative process of channel decoding (a receiver receives a block of bits, performs an operation and then checks if the block corresponds to parity rules. If the block doesn't correspond the operation is performed again and the process finally ends when the code corresponds to a given rules).

I have written the functions which perform the decoding process and return a m x n x i matrix where m x n is the block of data and i is the iteration index. So if it takes 3 iterations to decode the data the function returns a m x n x 3 matrix with each step is stired.

In the GUI (.fig file)开发者_Go百科 I put a "decode" button which runs the method for decoding and there are buttons "back" and "forward" which have to enable the user to switch between the data of recorded steps.

I have stored the "decodedData" matrix and currentStep value as a global variable so by clicking "forward" and "next" buttons the indices have to change and point to appropriate step states.

When I tried to debug the application the method returned the decoded data but when I tried to click "back" and "next" the decoded data appeared not to be declared.

Does anyone know how is it possible to access (or store) the results of the functions in order to enable the described logic which I want to implement in Matlab GUI?


Ultimately, this is a scoping of variables problem.

Global variables is rarely the right answer.

This video discusses the handles structure in GUIDE: http://blogs.mathworks.com/videos/2008/04/17/advanced-matlab-handles-and-other-inputs-to-guide-callbacks/

This video discusses sharing of variables between GUIs and could apply to a single GUI problem also. http://blogs.mathworks.com/videos/2005/10/03/guide-video-part-two/


The trick is to use nested functions so that they share the same workspace. Since I already started with an example in your last question, now I'm simply adding GUI controls to enable going forward/backward interactively, in addition to play/stop the animation:

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

You might find the slider functionality a bit buggy, but you get the idea :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜