3.0

3.0 | 4 ratings Rate this file 144 downloads (last 30 days) File Size: 2.77 KB File ID: #9447

findreplace

by Fahad Al Mahmood

 

25 Dec 2005 (Updated 27 Dec 2005)

Code covered by BSD License  

This function finds and replaces strings in a text file

Download Now | Watch this File

File Information
Description

SYNTAX:

findreplace(file,otext,ntext)
findreplace(file,otext,ntext,match)

file: text file name (with or without path)
otext: text to be replaced (old text)
ntext: replacing text (new text)
match: either (1) for match case or (0) to ignore case. Default value is (1)

Example:

findreplace('sample.txt','Moller','Moler');
findreplace('sample.txt','jake','Jack',0);
findreplace('sample.txt','continue it is','continue its',0);

MATLAB release MATLAB 7.1.0 (R14SP3)
Zip File Content  
Other Files sample.txt,
findreplace.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (4)
30 Jan 2006 x y

Great!
We love you!

19 Jul 2006 Chengzhou Wang

Hi,

After I ran this script to replace strings of my matlab files, I found that all my comment lines (with '%') are removed, which is not what I want. Can the author check why this is the case?

09 Apr 2007 ahmet anĂ½l dindar

I've just used it. I have a *.dat file in which I want to change a specific number. It changes efficiently, but somehow the lines are re-arranged as one after each other horizontally instead of vertically with a small square character. It confuses me te follow the inputs in the files after using find/replace. Therefore, I sugges the author to succed keeping the original file as it was after applying the .m code.

01 Jan 2008 Ellis King

The problem with removing '%' comment lines in .m files is caused by the fprintf function using the same character to indicate input. This function can be improved for use with .m files by inserting the additional check inside the 'for' loop, prior to the fprintf call at line 117:

if strcmp(fext, '.m')
    line{i} = strrep(line{i},'%','%%');
end

The logic ensures that the comments apply only for .m files.

Otherwise, it works fairly well.

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
strings Fahad Al Mahmood 22 Oct 2008 08:10:46
find replace text file string manipulation Fahad Al Mahmood 22 Oct 2008 08:10:46
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com