Clear Filters
Clear Filters

Modifying Text Data without changing the file format?

2 views (last 30 days)
Hi,
I have a file that includes 3 parts of data as the following:
1) Header: 5 lines of chars.
2) Coordinate Info: All numbers , in the form [ID x y z] , many lines (around 9000 lines).
3) Cell Info: All Numbers, in the form [ID node1 node2 node3 node4] , many lines too.
Also there is a keyword before 2nd and 3rd part (one line of chars).
What I need to do is to change only the 2nd part (Coordinate Info) with new calculated coordinates, that have the exact same structure and size, and leave everything else as it was. Pretty much like opening the file then copy new Coordinate Info over the old Coordinate Info.
Also I want to keep the same file format (file.key) if possible.
What is the faster way to do it?
Thanks in advance.
  2 Comments
Cedric
Cedric on 4 Nov 2013
What is the last line before block 2 with coordinates, as well as the first line after?
Mohamed Selim
Mohamed Selim on 4 Nov 2013
Last line before block 2 is '*NODE'.
And last line before block 3 '*ELEMENT_SOLID'.
So block 2 is confined between these two lines.

Sign in to comment.

Accepted Answer

Cedric
Cedric on 5 Nov 2013
Edited: Cedric on 6 Nov 2013
Here is a simple enough way to achieve this.
content = fileread( 'srcFile.txt' ) ;
block1 = regexp( content, '^.+?\*NODE[\r\n]*', 'match', 'once' ) ; % Edited.
block3 = regexp( content, '\*ELEMENT_SOLID.+?$', 'match', 'once' ) ;
data = [1 20 21 22; 2 30 31 32; 3 40 41 42] ; % Replacement data.
block2_new = sprintf( '%d %f %f %f\r\n', data.' ) ; % New block 2.
fid = fopen( 'dstFile.txt', 'w' ) ;
fwrite( fid, [block1, block2_new, block3] ) ;
fclose( fid ) ;
Using the following file content for srcFile.txt
A
B
C
D
*NODE
1 1 2 3
2 4 5 6
*ELEMENT_SOLID
9
10
11
X
Y
Z
We get the following dstFile.txt
A
B
C
D
*NODE
1 20.000000 21.000000 22.000000
2 30.000000 31.000000 32.000000
3 40.000000 41.000000 42.000000
*ELEMENT_SOLID
9
10
11
X
Y
Z
If you didn't want to deal with REGEXP, you could proceed as Walter mentions, using a combination of FGETL, FPRINTF, STRCMP or STRFIND.
  4 Comments
Cedric
Cedric on 6 Nov 2013
Follow-up: data after *NODE had padding which was caught by '\s*'; I changed it for '[\r\n]*'.

Sign in to comment.

More Answers (2)

per isakson
per isakson on 4 Nov 2013
Given "that have the exact same structure and size," memmapfile is the fastest.
  1 Comment
Mohamed Selim
Mohamed Selim on 4 Nov 2013
Can you elaborate more?
I tried to understand the idea of "memmapfile" by reading the help documentation, but it is not clear to me.

Sign in to comment.


Walter Roberson
Walter Roberson on 4 Nov 2013
Updating a file "in place" should only be done for binary files (or, in rare cases, when there is only pure overwriting without any text being inserted or deleted.)
As this file will be a text file, you should copy it line by line to another file until you get to the part you want to change, then write the revised information in the output file and then proceed copying the remaining part of the input file to the output. You can rename the output file to the old name afterwards.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!