Code covered by the BSD License  

Highlights from
Find & Replace System

image thumbnail

Find & Replace System

by

 

Searches any block/annotation/signal property in a model and replaces with the new value.

find_replace_system(system_name,prop_name,prop_value,new_value,varargin)
function find_replace_system(system_name,prop_name,prop_value,new_value,varargin)
% This function "find_replace_system" will search and replace almost any
% block/annotation/signal property in Simulink. 
% Only one property can be searched at a time, the value of the property
% that is matched will be replaced with a new value.
%
% Please refer the syntax and examples below.
%
% Syntax:
%   >>find_replace_system('<model name/subsystem name>','<property name>','<property value>','<replace value>','prompt')
%   >>find_replace_system('<model name/subsystem name>','<property name>','<property value>','<replace value>')
%       - 'prompt' is an optional keyword.
%
% Example:
%   >>find_replace_system('sldemo_autotrans','Gain','60/5280','60/1000','prompt')
%   >>find_replace_system('sldemo_autotrans/Vehicle','Name','Speed','Force','prompt')
%   >>find_replace_system('sldemo_autotrans','ForegroundColor','Red','Blue')
%   >>find_replace_system('sldemo_clutch','Position',[150 306 185 354],[140 296 195 364],'prompt')
%   >>find_replace_system(gcs,'SampleTime','-1','0.1','prompt')
%
% Developed by: Neelakanda Bharathiraja.
%

% Input validation.
help_needed = false;
prompt = false;
if nargin <4 || nargin >5
    help_needed  = true;
elseif nargin == 5
    if isequal(varargin{end},'prompt'),
        prompt = true;
    else
        help_needed  = true;
    end
end
% Datatype validation of the given property.
if ~((isnumeric(new_value) && isnumeric(prop_value)) || ...
        (ischar(new_value) && ischar(prop_value)))
    help_needed = true;
end

if help_needed
    disp('************************************************************');
    disp(['Error in ' upper(mfilename) ' usage.']);
    disp('************************************************************');
    help(mfilename);
    return;
end

% Do a search here.
try
    find_match = slfind('FindObjects',[1,1,1,1],system_name,'LookUnderMasks','on',...
        'FollowLinks','on','Regexp','on','CaseSensitive','off',prop_name,prop_value);
catch
    msgbox('There is no match for this search!!!','Find and Replace','help');
    return;
end

% Replace the block property.
if ~isempty(find_match)
    for ii = 1:length(find_match)
        old_value = get_param(find_match(ii).Handle,prop_name);
        if isnumeric(old_value) && isnumeric(prop_value)
            % Numeric property values.
            replace_value = new_value;
        else
            % String property values.
            replace_value = regexprep(old_value,prop_value,new_value,'ignorecase');
        end
        go_replace = 1;
        % Prompt the user before the change.
        if prompt
            hilite_system(find_match(ii).Handle,'find');
            ButtonName = questdlg(['Do you want to change the block ' upper(prop_name) '?'], ...
                'Find and Replace', ...
                'Yes', 'No', 'Stop', 'Yes');
            switch ButtonName,
                case 'Yes',
                    go_replace = 1;
                case 'No',
                    go_replace = 0;
                case 'Stop',
                    hilite_system(find_match(ii).Handle,'none');
                    break;
            end
            hilite_system(find_match(ii).Handle,'none');
        else
            go_replace = 1;
        end
        if go_replace
            try
                set_param(find_match(ii).Handle,prop_name,replace_value);
            catch
                % If the change is not successfully done, then prompt the
                % user again.
                hilite_system(find_match(ii).Handle,'find');
                ButtonName = questdlg(['The property ' upper(prop_name)  ' is not successfully modified!!!',...
                    'Do you want to continue?'], ...
                    'Find and Replace', ...
                    'Yes', 'Stop', 'Yes');
                switch ButtonName,
                    case 'Yes',
                    case 'Stop',
                        break;
                end
                hilite_system(find_match(ii).Handle,'none');
            end
        end
    end
else
    msgbox('There is no match for this search!!!','Find and Replace','help');
end

return;

Contact us