function   aaa = sviplot3(input, pre_state, expe, aft_state);
%This function is only used by sviterbi.c 
%
% See also SVIPLOT1, SVIPLOT2, SVIPLOT3.

%  Copyright 1996-2000 The MathWorks, Inc.
%       $Revision: 1.11 $

    % only for mexFunction 'sviterbi' callMATLAB.
    M = input(1);
    trace_num = input(2);
    expen_flag = input(3);
    fig_position = input(4);
    trace_flag = input(5);
    leng = input(6);

    handles = get(input(7), 'UserData');
    aaa = max(find(pre_state~=0));
    pre_state = pre_state(1:aaa);
    bbb = max(find(aft_state~=0));
    aft_state = aft_state(1:bbb);

    num_state = M;
    n_std_sta = 2^M;
    PowPowM = n_std_sta^2;
    expense = zeros(leng, PowPowM);
    for i=1:PowPowM
        expense(:,i) = expense(:,i) + expe(1+(i-1)*leng:leng+(i-1)*leng);
    end;

    plot_curv_x = [get(handles(2), 'XData'), NaN, -fig_position, NaN];
    plot_curv_y = [get(handles(2), 'YData'), NaN, -1000, NaN];
    dot_x = get(handles(4), 'XData');
    dot_y = get(handles(4), 'YData');

    % plot only; no computation function here.
    flipped = zeros(1, n_std_sta);
    for i = 1 : n_std_sta 
        flipped(i) =  bi2de(fliplr(de2bi(i-1, M)));
    end;
    for j2 = 1 : length(pre_state)
        jj = pre_state(j2) - 1;
        for j = 1 : n_std_sta
            tmp = expense(trace_num, (j-1)*n_std_sta+1: j*n_std_sta);
            if expen_flag
                tmp_x = max(tmp(find(~isnan(tmp))));
            else
                tmp_x = min(tmp(find(~isnan(tmp))));
            end;

            if isempty(tmp_x)
                expen_rec(j) = NaN;
            else
                expen_rec(j) = tmp_x;
            end

            for j2 = 1 : length(tmp)
                if ~isnan(tmp(j2))
                    plot_curv_x = [plot_curv_x, fig_position-1+.1, fig_position, NaN];
                    plot_curv_y = [plot_curv_y, flipped(j2), flipped(j), NaN];
                end;
            end;
        end;
    end;
    for j2 = 1 : length(aft_state)
        jj = aft_state(j2) - 1;
        set(handles(jj+5), 'Position', [fig_position, bi2de(fliplr(de2bi(jj,num_state))), 0],...
           'String',  num2str(expen_rec(jj+1)));
        tmp = get(handles(jj+5), 'Position');
        dot_x = [dot_x, tmp(1)];
        dot_y = [dot_y, tmp(2)];
    end;

    set(handles(2), 'XData', plot_curv_x, 'YData', plot_curv_y);
    set(handles(4), 'XData', dot_x, 'YData', dot_y);
    if ~trace_flag
   %     drawnow;
    end;
    % end of sviplot4.m


