function FindandReplace(Inp)
% **********************************************************************
% File Name : FindandReplace
% Author : Shanmugam Kannappan
% Description : This is the function called when the
% "O.K" button is pressed from the FndRep_GUI.
% This function takes model in which the signals to be replaced &
% replaces the signal either from excel file or from editbox in
% FndRep_GUI(String option)
%***********************************************************************
try
mdl = bdroot; % Top level model name
mdlh = get_param(mdl,'Handle'); % Get the handle of the model
Filename = get(mdlh ,'FileName');% Get the filename of the model(path & file name)
fid = fopen(Filename,'r');% Open the model in read mode
str = fread(fid, '*char')';% Read the content of the model & assign in a variable
fclose(fid);% Close the file
[mdlPath] = fileparts(Filename);% Get the path & ceate a new directory called 'Old_model' if its not exist.
if ~exist([mdlPath,'\Old_model'],'dir')
mkdir([mdlPath,'\Old_model'])
end
close_system(Filename) % Close the model
clear fid mdlh % clear the handles
copyfile(Filename,[mdlPath,'\Old_model']) % Copy the current model to the directory('Old_Model') for safety purpose.
delete(Filename) % delete the model
if ~iscell(Inp) % if its not a cell(Excel option is selected)
[Num,Text] = xlsread(Inp);% Read the strings from Excel sheet & replace one by one
fid = fopen(Filename,'w+');% Open the file in write mode
for i=1:size(Text,1)
frewind(fid)
fnd = Text{i,1};
Rep = Text{i,2};
str = regexprep(str, fnd, Rep); % Replace the Signals
end
fprintf(fid, '%s', str); % Create a model
fclose(fid); % Close the file
else % String option is selected
fid = fopen(Filename,'w+'); % Open the file in write mode
frewind(fid)
str = regexprep(str, Inp{1,1}, Inp{1,2}); % Replace the Signals
fprintf(fid, '%s', str); % Create a model
fclose(fid); % Close the file
end
open_system(Filename) % open the model
catch % Current model is not saved
msgbox('Please check the model is saved & open','Check the model','Error');
end
clc
clear