www.gusucode.com > mbctools 工具箱 matlab 源码程序 > mbctools/@sweepsetfilter/resampleEditDlg.m

    function [OK, ssf] = resampleEditDlg(ssf, hParent, index)
%RESAMPLEEDITDLG Show a dialog to edit a resampling expression
%
%  [OK, SSF] = RESAMPLEEDITDLG(SSF, HPARENT, INDEX) displays a dialog that
%  allows you to add, edit or delete the resampling expressions associated
%  with a sweepsetfilter.  OK indicates whether the user clicked Ok (true)
%  or Cancel (false).
%
%  Examples:
%    [ok, ssf] = RESAMPLEEDITDLG( ssf, fh )
%    [ok, ssf] = RESAMPLEEDITDLG( ssf, fh, 2 )
%
%  See also: SWEEPSETFILTER, SWEEPSETFILTER/EQUATIONEDITDLG.

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

% Check input arguments
if nargin < 2 || isempty( hParent )
    hParent = 1;
end
if nargin < 3
    resampling = get( ssf, 'Resampling' );
    index = min(1, size(resampling.resampleExp,1));
end

data = struct(...
    'TitleString', 'Resample Editor', ...
    'FigureSize', [590, 300], ...
    'ListString', 'Currently defined resamplings:',...
    'HelpID', '',...
    'InitialIndex', index,...
    'NewItemString', 'New Resampling %d', ...
    'AddVariableFmt', '%s in.%s', ... % must contain two '%s'
    'HelpString', {{ ...
    'Specify the equation of the resample expression, e.g., resample( x, 5, 7 ).', ...
    'Use ''in.varName'' and ''out.varName'' to specify the variable ''newVar'' from', ...
    'the input or output side. Use ''x'' as a place holder any input side variable.' }} );

% Run the resampling expression editor
[OK, ssf] = i_createDialog(ssf, hParent, data);
end

% --------------------------------------------------------------------
function [OK, ssf] = i_createDialog(ssf, hParent, data)
% Check that the resampling editor doesn't exist
fh = mvf( 'ResamplingEditor' );
if ~isempty(fh)
    delete(fh)
end

% Create runtime pointers to hold relevant data
pHandles = xregGui.RunTimePointer;

f = xregdialog('name', data.TitleString,...
    'tag', 'ResamplingEditor',...
    'pointer', 'watch',...
    'MinimumSize', data.FigureSize );

% Center the figure on the parent figure
xregcenterfigure(f, data.FigureSize, hParent);

pHandles.LinkToObject(f);

% Set the default outputs correctly
INITIAL_RESAMPLING = get( ssf, 'serializeresampling' );
f.UserData = struct( ...
    'resampling', {INITIAL_RESAMPLING} );

% Create the layout (based on the data)
L = i_createLayout(f, data, pHandles);

% Add OK/Cancel buttons
OK = false;
    function i_setOK(~, ~)
        OK = true;
        f.Visible = 'off';
    end
btOK = uicontrol('Parent',f,...
    'Style','pushbutton',...
    'Callback', @i_setOK,...
    'String','OK');
btCancel = uicontrol('Parent',f,...
    'Style','pushbutton',...
    'Callback',@(src, evt) set(f, 'Visible', 'off'),...
    'String','Cancel');
btL = xreggridbaglayout(f, ...
    'dimension', [2 3], ...
    'rowsizes', [-1 25], ...
    'colsizes', [-1 65 65], ...
    'gap', 7, ...
    'border', [7 7 7 7], ...
    'mergeblock', {[1 1], [1 3]}, ...
    'elements', {L, [], [], btOK, [], btCancel});

f.LayoutManager = btL;
set(btL, 'packstatus', 'on');

% Fill in the data
i_updateData(ssf, pHandles, data);

f.Pointer = 'arrow';
f.showDialog(btOK);

% Update the SSF with the new resampling (if it's changed)
resampling = f.UserData.resampling;
if OK && ~isequal( INITIAL_RESAMPLING, resampling )
    hWait = xregGui.waitdlg( 'Parent', hParent, ...
        'Message', 'Parsing resample expressions...', ...
        'Title', 'Data Editor');

    ssf = removeResampling( ssf, ':' );
    nResampling = size( resampling, 1 );
    hWait.Waitbar.Max = nResampling;
    for i = 1:nResampling,
        ssf = addResampling( ssf, resampling{i,:} );
        hWait.Waitbar.Value = i;
    end
    delete( hWait );
end

delete(f);
end


% --------------------------------------------------------------------
function layout = i_createLayout(fh, data, pHandles)
SC = xregGui.SystemColorsDbl;

h.figure = fh;

txHelp = uicontrol('Parent', fh,...
    'Style', 'text',...
    'String', data.HelpString,...
    'HorizontalAlignment','left');
helpHeight = 15 * length(data.HelpString);

h.out1 = uicontrol('Parent',fh,...
    'Style','edit',...
    'BackgroundColor', SC.WINDOW_BG,...
    'HorizontalAlignment','left',...
    'Tag', 'editBox',...
    'Callback', {@i_outEdited, pHandles, data});

% General name for the editing area
editor = h.out1;
editorHeight = 20;

% List view for variables
cols = {'','Variable', 'Min', 'Max', 'Mean', 'Units'};
width = [15, 100, 50, 50, 50, 50];

pnl1 = mbcgui.container.layoutpanel('Parent', fh);
h.lvVars = mbcwidgets.List('SelectionMode', 'SingleRow',...
    'Grid', 1,...
    'ColumnHeaders', cols,...
    'ColumnWidths', width,...
    ...%'Editable', true,...
    'Parent', pnl1);
set(pnl1, 'LayoutComponent', {h.lvVars})

h.lvVars.Peer.setDisplayChecks(true);
set(h.lvVars, 'ActionPerformedCallback', {@i_listviewDoubleClick pHandles data});
set(h.lvVars, 'ValueChangedCallback', {@i_outEdited, pHandles, data});


% List of all resample expressions
txList = uicontrol('Parent', fh,...
    'Style', 'text',...
    'String', data.ListString,...
    'HorizontalAlignment','left');

h.list = xregGui.listeditor(fh,...
    'AddItemMode', 'unboundlist',...
    'BackgroundColor', SC.WINDOW_BG,...
    'NewItemTemplate', data.NewItemString,...
    'ListSelectionFcn', {@i_listSelection pHandles data},...
    'ListReorderFcn', {@i_listReorder pHandles data},...
    'AddItemFcn', {@i_listAddItem pHandles data},...
    'DeleteItemFcn', {@i_listDeleteItem pHandles data});

layout = xreggridbaglayout(fh,...
    'packstatus', 'off', ...
    'dimension', [4, 2],...
    'rowsizes', [15, helpHeight-20, editorHeight, -1],...
    'colsizes', [200 -1],...
    'gapx', 20, ...
    'gapy', 5, ...
    'mergeblock', {[1, 2] [2, 2]},...
    'mergeblock', {[2, 4] [1, 1]}, ...
    'elements', {txList h.list [] [], ...
    txHelp [] editor pnl1});

% Set the handle data
pHandles.info = h;
end


% --------------------------------------------------------------------
function i_updateData(ssf, pHandles, data)

% Get the resampling information
resampling = get(ssf, 'resampling');

% Fill in the list
list = pHandles.info.list;
[strings{1:length(resampling.resampleExp)}] = deal( resampling.resampleExp{:} );
list.ItemList = strings;
list.Value = 1:length(strings);

% Fill in the current equation
if isempty(data.InitialIndex)
    data.InitialIndex = 1;
end
list.SelectedItem = data.InitialIndex;

% Get the variable information
ss = sweepset( setAllows( ssf, 'Resampling', 'off' ) );
out = get(ss, {'Name' 'Units'});
names = out{1};
units = out{2};
ssData  = double(ss);

% Check that there actually is some data
if isempty(ssData)
    ssData = NaN*ones(1, length(names));
end
mn   = min(ssData, [], 1);
mx   = max(ssData, [], 1);
ave  = mean(ssData, 1);

% Find which variables match the current expression
if data.InitialIndex>0
    checkVar = ismember( names, resampling.varNames{data.InitialIndex} );
else
    checkVar = true(size(names));
end
emptyColumn = repmat({' '}, size(names));
data = [emptyColumn, names, num2cell([mn', mx', ave']), units];
setData(pHandles.info.lvVars, data)
pHandles.info.lvVars.Peer.setChecks(checkVar)

% Update the equation
i_listSelection([], [], pHandles, data);
end

% --------------------------------------------------------------------
% Listview double clicked so add the text to the end of the out1 text
% --------------------------------------------------------------------
function i_listviewDoubleClick(src, event, pHandles, data)
% Only respond to a double click if there is at least one object in the list
if ~isempty(pHandles.info.list.ItemList)
    out1 = pHandles.info.out1;
    selectedVar = event.data.SelectedRows;
    varName = src.data{selectedVar,1};
    set(out1, 'String', ...
        sprintf( data.AddVariableFmt, get(out1, 'String'), varName ));
    i_outEdited(src, event, pHandles, data);
end
end


% --------------------------------------------------------------------
function i_listSelection(~, ~, pHandles, data)
f = pHandles.info.figure;
ud = get(f, 'UserData');
list = pHandles.info.list;
i_setEditor(pHandles, data, ud.resampling, list.SelectedItem);
end


% --------------------------------------------------------------------
function i_setEditor(pHandles, ~, resampling, index) 

if isempty( index ) || index < 1,
    set(pHandles.info.out1, 'String', '', 'Enable', 'off');
else
    set( pHandles.info.out1, ...
        'String', resampling{index,2}, ...
        'Enable', 'on' );
    i_checkVariables( pHandles, resampling{index,1} );
end
end


% --------------------------------------------------------------------
function i_listAddItem(~, ~, pHandles, data)
f = pHandles.info.figure;
ud = get(f, 'UserData');
list = pHandles.info.list;

resampling = f.UserData.resampling;
resampling{end+1,1} = i_chosenVariables( pHandles );
resampling{end  ,2} = 'x';
ud.resampling = resampling;
set(f, 'UserData', ud);

% Ensure the list is ordered first to last
list.Value = 1:length(list.Value);

% Update the equation
i_listSelection([], [], pHandles, data);
end


% --------------------------------------------------------------------
function i_listDeleteItem(~, event, pHandles, data)
f = pHandles.info.figure;
ud = get(f, 'UserData');
% Remove the item from the sweepsetfilter
index = event.data.ItemDeleted;
ud.resampling(index,:) = [];
set(f, 'UserData', ud);
% Update the equation
i_listSelection([], [], pHandles, data);
end


% --------------------------------------------------------------------
function i_listReorder(~, ~, pHandles, ~)
f = pHandles.info.figure;
ud = get(f, 'UserData');
list = pHandles.info.list;
index = list.value;
% Reorder the resamplings
ud.resampling = ud.resampling(index,:);
set(f, 'UserData', ud);
% Reorder the Itemlist
list.ItemList = list.ItemList(index);
% Ensure the list is ordered first to last
list.Value = 1:length(list.Value);
end


% --------------------------------------------------------------------
function varNames = i_chosenVariables( pHandles )
list = pHandles.info.lvVars;
varnameColumn = strcmpi(list.ColumnHeaders, 'Variable');
ischecked = list.Peer.getChecks;
data = list.Data;
varNames = data(ischecked, varnameColumn);

end


% --------------------------------------------------------------------
function i_checkVariables( pHandles, varNames )
if ~iscell( varNames ),
    varNames = {varNames};
end
list = pHandles.info.lvVars;
data = list.Data;
varnameColumn = strcmpi(list.ColumnHeaders, 'Variable');
allVars = data(:, varnameColumn);
checkVars = ismember(allVars, varNames);
list.Peer.setChecks(checkVars);

end

% --------------------------------------------------------------------
function i_outEdited(~, ~, pHandles, data)
f = pHandles.info.figure;
ud = get(f, 'UserData');
out1 = pHandles.info.out1;
list = pHandles.info.list;

index = list.SelectedItem;

if index>0
    [ud.resampling{index,:}] = deal( ...
        i_chosenVariables( pHandles ), ... % var names
        get(out1, 'String') ); % resampling expression
    set(f, 'UserData', ud);

    % Update the list
    list.ItemList{list.SelectedItem} = get(out1, 'String');

    % Update the equation
    i_listSelection([], [], pHandles, data);
end
end