www.gusucode.com > mbcexpr 工具箱 matlab 源码程序 > mbcexpr/@cgexprgui/@BasicSliceGraph/update.m

    function update(h)
%UPDATE Update the expression plot
%
%  UPDATE(H) redraws the expression evaluation plot.

%  Copyright 2000-2013 The MathWorks, Inc. and Ford Global Technologies, Inc.


pExpr = h.Expression;

% Always update the title of the plot
if isnull(pExpr)   
    nm = '';
else
    nm = getname(pExpr.info);
end
set(h.Display, 'BarTitle', nm);

if ~isnull(pExpr)
    expr = pExpr.info;
    pGrid = h.pGetGridVars;

    % evaluate the model
    data = evaluategrid(expr, pGrid,'value');
    if h.DisplayConstraints>0 && concheck(expr)
        constraintdata = evaluategrid(expr, pGrid, 'constraint');
    else
        constraintdata = [];
    end

    % Check for conditions that trigger a message instead of a plot
    DoPlot= true;
    NoPlotMsg = '';
    if all(isnan(data(:)))
        DoPlot = false;
        NoPlotMsg = 'Unable to plot data.  The model output produces NaN values.';
    elseif ~any(isfinite(data(:)))
        DoPlot = false;
        NoPlotMsg = 'Unable to plot data.  The model output has no finite values.';
    else
        % convert all non-finite to NaN - avoids troubles with rotating inf graphs
        data(~isfinite(data)) = NaN;
        % deal with complex data
        data(~isreal(data)) = NaN;
    end


    if DoPlot
        PlotStyle = min(h.GUIstate,2);
        switch PlotStyle
            case 0
                set(h.line,'XData',[],'YData',[]);
                set(h.IntervalPatch, 'OpenIntervals', NaN);
                i_clear3d(h.surface, h.patch);
                if ~isempty(constraintdata)
                    if constraintdata>0
                        constr = 'outside';
                    else
                        constr = 'inside';
                    end
                    set(h.message, 'String', ...
                        sprintf('Model value: %g\nValue is %s the constraint boundary.', data, constr));
                else
                    set(h.message, 'String', sprintf('Model value: %g', data));
                end
            case 1
                i_clear3d(h.surface, h.patch);
                
                xd = pGrid(1).getvalue;
                xL = pGrid(1).getrange;
                yL = mbcmakelimits(data, 'loose');
                con = [];
                xcon = [];
                if ~isempty(constraintdata)
                    if h.DisplayConstraints==1
                        con = constraintdata;
                        xcon = xd;
                    elseif h.DisplayConstraints==2
                        xd = xd(constraintdata<=0);
                        data = data(constraintdata<=0);
                    end
                end
                set(h.line,'XData', xd, 'YData',data);
                h.IntervalPatch.setConstraintVector(xcon, con);
                mbcxlabel(h.Axes2D, pGrid(1).getname);
                set(h.Axes2D,'XLim',xL,'YLim',yL);
            case 2
                set(h.line,'XData',[],'YData',[]);
                set(h.IntervalPatch, 'OpenIntervals', NaN);

                xL = pGrid(1).getrange;
                yL = pGrid(2).getrange;
                zL = mbcmakelimits(data);

                [X,Y] = ndgrid(pGrid(1).getvalue, pGrid(2).getvalue);
                if ~isempty(constraintdata)
                    % Create a constrained patch version
                    if ~isempty(h.surface) && isgraphics(h.surface)
                        delete(h.surface);
                    end
                    P = [];
                    if ~isempty(h.patch) && isgraphics(h.patch)
                        P = h.patch;
                    end

                    % Flag that indicates whether to display or clip to
                    % constraints
                    ShowCon = 'on';
                    if h.DisplayConstraints==2
                        ShowCon = 'off';
                    end

                    h.patch = xregsurfaceb(X, Y, data, ...
                        constraintdata, i_makecdata(data, h.colormap, zL), ...
                        'DisplayExterior', ShowCon, ...
                        'Patch', P, ...
                        'parent', h.Axes3D, ...
                        'facelighting', 'gouraud', ...
                        'facecolor', 'interp', ...
                        'edgecolor', 'none');
                else
                    % Use a standard surface object
                    if isempty(h.surface) || ~isgraphics(h.surface)
                        % Need to create a surface and possibly destroy a
                        % boundary display patch
                        if ~isempty(h.patch) && isgraphics(h.patch)
                            delete(h.patch);
                        end
                        h.surface = surface('Parent',h.Axes3D,...
                            'FaceLighting','gouraud',...
                            'FaceColor','interp',...
                            'EdgeColor','none',...
                            'XData',X,...
                            'YData',Y,...
                            'ZData',data,...
                            'CData',i_makecdata(data, h.colormap, zL));
                    else
                        set(h.surface,'XData',X,...
                            'YData',Y,...
                            'ZData',data,...
                            'CData',i_makecdata(data, h.colormap, zL));
                    end
                end
                mbcxlabel(h.Axes3D, pGrid(1).getname);
                mbcylabel(h.Axes3D, pGrid(2).getname);
                set(h.Axes3D, 'XLim',xL, 'YLim',yL, 'ZLim', zL);
        end

        set(h.axesPane,'currentcard',PlotStyle+1);
    else
        set(h.message, 'String', NoPlotMsg);
        set(h.axesPane,'currentcard',1);
    end
end


function i_clear3d(hSurf, hPatch)
if ~isempty(hSurf) && isgraphics(hSurf)
    set(hSurf,'XData',[],'YData',[],'ZData',[],'CData',[]);
elseif ~isempty(hPatch) && isgraphics(hPatch)
    delete(hPatch);
end


function cdata = i_makecdata(zdata, cmap, clim)
n = size(cmap,1);
mn = clim(1);
mx = clim(2);

% work out cdata - needs to be one for each vertex (zdata)
edges = (mn:((mx-mn)/n):mx);
[unused, bin] = histc(zdata,[-inf edges(2:end-1) inf]);

% Convert bin entries for NaN zdata (bin 0) into using NaN cdata
cmap = [cmap; NaN NaN NaN];
bin(bin==0) = n+1;

cdata = reshape(cmap(bin(:),:), [size(zdata) 3]);