www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/@cgoptim/guiSelectOpVars.m

    function [optim,OK] = guiSelectOpVars(optim)
%guiSelectOpVars Graphically select operating point variables
%
%   [optim,OK] = guiSelectOpVars(optim) displays a dialog that allows you
%   to select the two fixed variables that should be used as the
%   interpolation basis for application point sets.

%  Copyright 2009-2015 The MathWorks, Inc.

OK = false;

% sort out mode variable
optim = initOpPointVariables(optim);

DS = optim.oppoints;
if isempty(DS.OpPointVariables)
    h = errordlg('There must be at least two fixed variables in the optimization.',...
        'Operating Point Set','modal');
    uiwait(h)
    return
end

pfixed = getfixedvalues(optim);

% Display the dialog
[pOpVars, OK] = iShowDialog(optim, DS.OpPointVariables, pfixed);

if OK
    % Insert the variables into the optimization
    DS = editOpPointVariables(DS,pOpVars);
    optim.oppoints = DS;
end

end


function [pOpVars, OK] = iShowDialog(optim, pdef, pfixed)


SC = xregGui.SystemColorsDbl;
editbg = SC.WINDOW_BG;

infostr = ['Select the operating point variables for all of the application ', ...
    'point sets in the optimization.'];

hDlg = mbcgui.container.Dialog('Name', 'Select Operating Point Variables', ...
    'Tag', 'OptimApplicationVars', ...
    'Buttons', 'OK_CANCEL_HELP',...
    'Size', [340 400], ...
    'PersistPosition', false, ...
    'HelpCode', 'CGOPTIMAPPLICATIONPOINTS', ...
    'InfoString', infostr);
hFig = hDlg.Figure;

nOps = 2;
hOpLabel = cell(1,nOps);
hOpVar = cell(1,nOps);
Labels = {'X Variable:','Y Variable:'};
Options = pveceval(pfixed,@getname);
for i = 1:nOps
    hOpVar{i} = uicontrol('Parent',hFig,...
        'Style','popupmenu',...
        'BackgroundColor',editbg,...
        'String', Options,...
        'Value', findindex(pdef, pfixed, i),...
        'Tag', Labels{i}, ...
        'Callback', {@cbkOpVar,i});
    hOpLabel{i} = xregGui.labelcontrol('parent', hFig, ...
        'String', Labels{i}, ...
        'LabelSizeMode', 'absolute', ...
        'ControlSizeMode', 'relative', ...
        'LabelSize', 60, ...
        'ControlSize', 1, ...
        'Control', hOpVar{i});
end

hAxesPanel = mbcgui.widget.AxesPanel(...
    'Parent', hFig, ...
    'Border', [50 50 20 20]);
hAxes = hAxesPanel.AxesHandle;
set(hAxes, ...
    'Box', 'on', ...
    'XGrid', 'on', ...
    'YGrid', 'on');
mbctitle(hAxes, 'Optimization points');
hOptimLine = line('Parent',hAxes,...
    'Marker', 'x', ...
    'MarkerSize', 10, ...
    'MarkerEdgeColor', [.4 .4 1], ...
    'LineWidth',2,...
    'LineStyle', 'none', ...
    'DisplayName', 'Application Point Set', ...
    'Clipping','off');

hWarnText = text('Parent', hAxes, ...
    'BackgroundColor', [1 1 .57], ...
    'EdgeColor', 'k', ...
    'String', sprintf('The selected variables contain\n different numbers of values.'), ...
    'Visible', 'off', ...
    'HorizontalAlignment', 'center', ...
    'Clipping', 'on', ...
    'Position', [.5 .5 0]);

hDlg.Content = xreggridbaglayout(hFig,...
    'dimension',[2 2],...
    'rowsizes',[-1 20],...
    'gapx',20,...
    'mergeblock',{[1 1],[1 2]},...
    'Border', [0 10 0 10], ...
    'Elements',{hAxesPanel,hOpLabel{1},[],hOpLabel{2}});

updateAxes;

closeMode = hDlg.showDialog;

OK = strcmp(closeMode, 'OK');
pOpVars = pdef;
for i = 1:nOps
    pOpVars(i) = pfixed(get(hOpVar{i},'Value'));
end

delete(hDlg);


    function cbkOpVar(hOp,~,ctrl)
        % check other operating point variable
        switch ctrl
            case 1
                hOther = hOpVar{2};
            case 2
                hOther = hOpVar{1};
        end
        if get(hOp,'Value')==get(hOther,'Value')
            Other = setdiff(1:length(pfixed), get(hOp,'Value'));
            set(hOther,'Value',Other(1));
        end
        updateAxes;
    end

    function updateAxes
        p1 = pfixed(get(hOpVar{1},'Value'));
        p2 = pfixed(get(hOpVar{2},'Value'));
        
        data = getinitialvaluedata(optim, [p1 p2]);
        data = cellfun(@(c) c(:), data, 'UniformOutput', false);
        if length(data{1})==length(data{2})
            % The two variables match in length
            set(hOptimLine, 'XData', data{1}, 'YData', data{2});
            set(hAxes, 'XGrid', 'on', 'YGrid', 'on');
            
            % Hide warning
            set(hWarnText, 'Visible', 'off');
        else
            set(hOptimLine, 'XData', [], 'YData', []);
            
            % Show a warning
            set(hAxes, 'XGrid', 'off', 'YGrid', 'off');
            set(hWarnText, 'Visible', 'on');
        end
        
        mbcxlabel(hAxes,p1.getname,'Interpreter','none');
        mbcylabel(hAxes,p2.getname,'Interpreter','none');
    end

end