image thumbnail

Remove Sink Blocks

by

 

This function automatically removes all sink blocks from a model.

RemoveSinks(modelname, varargin)
function RemoveSinks(modelname, varargin)
% CloseAllScopes.m
% Mike Anthony
% Copyright (c) 2013, The MathWorks, Inc.
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are
% met:
%
%     * Redistributions of source code must retain the above copyright
%       notice, this list of conditions and the following disclaimer.
%     * Redistributions in binary form must reproduce the above copyright
%       notice, this list of conditions and the following disclaimer in
%       the documentation and/or other materials provided with the distribution
%     * Neither the name of the The MathWorks, Inc. nor the names
%       of its contributors may be used to endorse or promote products derived
%       from this software without specific prior written permission.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
% POSSIBILITY OF SUCH DAMAGE.
%
%
% RemoveSinks(modelname, varargin)
%
% Remove selected types of Sink blocks from a model and/or model reference
% hierarchy.

if isempty(varargin)
    % Ask questions
    lib_opts = {'Scopes & Floating Scopes'
        'Display'
        'ToWorkspace'
        'ToFile'
        'XY Graph'};
    
    [button_ans, check] = listdlg('ListString',lib_opts,'SelectionMode','multiple','ListSize',[200 80],'PromptString','Delete which blocks?');
    
    % If user cancels or close dialog, or selects a blank, stop
    if check == 0
        return
    end
       
else
    button_ans = varargin{1};
end

% Ask if you want to traverse Model Reference Hierarchy
mrs = find_system(modelname,'LookUnderMasks','all','BlockType','ModelReference');
if ~isempty(mrs)
    if isempty(varargin)
        mr_ans = questdlg(['Remove Sinks in Referenced Models?' sprintf('\n') sprintf('\n') 'NO: Only sinks in top-level model are deleted' sprintf('\n') 'OPEN: Sinks in any currently open Referenced Models are also deleted' sprintf('\n') 'ALL: All Referenced Models will be opened and sinks deleted'],'ModelRef Question','No','Open','All','No');
    else
        mr_ans = varargin{2};
    end
else
    mr_ans = 'No';
end
    
if ~strcmp(mr_ans,'No')
    for m = 1:length(mrs)
        % Get name of Referenced Model
        mr_name = get_param(mrs{m},'ModelName');
        
        % Check if Referenced Model is open
        try
            if strcmp(mr_ans,'All')
%                 load_system(mr_name);
                open_system(mr_name);
            end
            
            m_isopen = find_system(mr_name);
            
            % Recursive call into referenced models
            RemoveSinks(mr_name, button_ans, mr_ans);
            
%             if strcmp(mr_ans,'All')
%                 close_system(mr_name);
%             end
        catch
        end
    end
end

% Find all the scopes, floating scopes, displays, ToWorkspaces, ToFiles
sinks = {};
if any(button_ans == 1)
    blks = find_system(modelname,'LookUnderMasks','all','BlockType','Scope');
    sinks  = {sinks{:} blks{:}};
end
if any(button_ans == 2)
    blks   = find_system(modelname,'LookUnderMasks','all','BlockType','Display');
    sinks  = {sinks{:} blks{:}};
end
if any(button_ans == 3)
    blks   = find_system(modelname,'LookUnderMasks','all','BlockType','ToWorkspace');
    sinks  = {sinks{:} blks{:}};
end
if any(button_ans == 4)
    blks   = find_system(modelname,'LookUnderMasks','all','BlockType','ToFile');
    sinks  = {sinks{:} blks{:}};
end
if any(button_ans == 5)
    blks   = find_system(modelname,'LookUnderMasks','all','MaskType','XY scope.');
    sinks  = {sinks{:} blks{:}};
end

for i = 1:length(sinks)
    port = get_param(sinks{i},'PortHandles');
    dad = get_param(sinks{i},'Parent');
    % Floating Scopes have no inputs
    if ~isempty(port.Inport)
        % XY Graphs have 2 inputs
        for p = 1:length(port.Inport)
            % Get the port position
            pos = get_param(port.Inport(p),'Position');
            try
                % Delete a line that exists in that position
                delete_line(dad,pos);
            catch
                % Something bad happened
                disp(['Problem with block: ' sinks{i}])
            end
        end
    end
    delete_block(sinks{i});
end

Contact us