function f = replace_str_in_files(datapath_str, file_ext, srch_str, repl_str)

% All input arguments are strings.
% Invokes perl script to search and replace in all ascii files
% in the datapath_str folder with specified extension.
% Useful to globally convert NAN to NaN, for example.
% Creates backup files as .bak, which are deleted by default but
% you can comment that line out to save them if you wish.
% *** perl language must be installed on the computer ***
% Jan 2015 BWB

ext_spec = ['*.',file_ext];
files = dir(fullfile(datapath_str,ext_spec)); % get list of file names
N = length(files);
for ii=1:N % iterate through all files & search/replace
    fname = files(ii).name;
    cmd_str = ['perl -i.bak -pe "s/',srch_str,'/',repl_str,'/g" ',fullfile(datapath_str,fname)];
    f = system(cmd_str);
end;
delete(fullfile(datapath_str,'*.txt.bak')); % comment to retain bak copies

