MATLAB Answers


To replace a character AT A PARTICULAR POSITION in a Text FIle.

Asked by Priya
on 15 Apr 2013

I have a Text File containing many characters, I wish to modify one of those characters at a particular position

Suppose, I have a text file "1ABC.txt"

Contents of 1ABC.txt are as follows:



from = {D,K,F};

to = {E,I,A};

position = {2,4,10};

For position in variable "position",

I have to check if the character at that position in 1ABC.txt is the same as in variable "from".

If that character is same , then I have to replace it with the character in variable "to".

Then I have to store this new line (after replacing original character in 1ABC.txt) in a different file "new.txt".

Then it will check for other positions in position variable and store the new line after replacement as a next line in the file "new.txt".

E.g. : Program will check if at the second position there is 'D' in 1ABC.txt.

Since, there is 'D' at second position of 1ABC.txt, so it will replace it with 'E'.

Save the text as new.txt after replacement and subsequent replacements as new line in "new.txt" so on loop will run .....


For the second change, should the line output have only the second change, or should it have the effects of both the first and the second change?

If there is not a match at a position, then the line is still to be output, right?

Why doing this using files? It would be much more efficient to follow the iterative process in memory, and export to file the whole history if necessary.


No products are associated with this question.

3 Answers

Answer by Cedric Wannaz
on 15 Apr 2013
Edited by Cedric Wannaz
on 15 Apr 2013
 Accepted answer

I see that you accepted 0% of the answers that were given to your 4 previous questions; please take a moment for reviewing these questions/answers and accept relevant answers.

As mentioned in my comment below your question, working with memory would be much more efficient if you follow an iterative process. However, here is an example illustrating one way to achieve what you want using regular expressions:

 % - Read source file.
 buffer  = fileread('1ABC.txt') ;
 % - Extract {string, from, to, position} from content => struct.
 pattern = '(?<string>^\S*).*?{(?<from>.*?)}.*?{(?<to>.*?)}.*?{(?<position>.*?)}' ;
 content = regexp(buffer, pattern, 'names') ;
 content.position = str2num(content.position) ;
 % - Replace characters when relevant.
 for k = 1 : 3
    if buffer(content.position(k)) == content.from(1+2*(k-1))
        buffer(content.position(k)) =*(k-1)) ;
 % - Export to destination file.
 fid = fopen('new.txt', 'w') ;
 fwrite(fid, buffer) ;
 fclose(fid) ;

Note that it would be possible to make the whole pattern matching and replacement as a single call to REGEXPREP I guess, but the pattern would be significantly more complicated.

  1 Comment

on 21 Apr 2013

Thanks , your code helped a lot in debugging my code..

Answer by Yao Li
on 15 Apr 2013




Unfortunately it is not possible to seek by line number. It is possible to find some other way to determine which character position in the file is affected and then fseek() to that character position; this can be especially useful if the same character will need to be modified over and over. When determining which character position is to be modified, take into account the line terminators.

Yao Li
on 15 Apr 2013

Since he has already obtained the position of the character which needs to be replaced (the 5th), I think the function fseek is feasible. However, if he has to get the position first, I think a while loop and the fuction fgetl() may be helpful.

Answer by Walter Roberson
on 15 Apr 2013

When you fopen(), use 'a+' as the access type.

fseek() to the beginning of file if you are going to be reading from the beginning as your method of positioning; if you already know the exact offset you are positioning to you can fseek() to there. If you arrived at your desired location by reading (rather than fseek() directly there) then you must fseek() to the position you are already in (0 bytes offset relative to current position) before you can write to the file. To emphasize, you need to fseek() if you are switching between reading and writing or between writing and reading.

Once you are at the correct location, you can fwrite() the replacement character in place. (fprintf() could also be used but using it tempts the programmer to think in terms of the output content rather than in terms of replacing an exact number of bytes.)

You can fclose() immediately after the fwrite(). Or you can fseek() to a different position for more changes.


Join the 15-year community celebration.

Play games and win prizes!

Learn more
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

MATLAB Academy

New to MATLAB?

Learn MATLAB today!