Reading and Writing some parts of a line in another text file in a preferred format in Matlab

2 views (last 30 days)
Hello--
I do appreciate any detailed helps. I really am in a terrible situation and I would be honored if anyone can help me with this issue in a great details!
Thanks in advance!
Well! I have a large text file that is made of group of 209 rows! In another words in my large file there is simple element with the following format that repeats many times (let us name it NR) . each element has 209 rows and 5 columns. I am interested in having the data corresponding to the last three columns for 6 specific rows in each element. these 6 rows ( let me call them r1 to r6) are constant for all of the NR loops.
The third column which is the first column of interest starts at character number 25 of the row i.e. cell number 25 and ends at character number 37. The forth column which is the second column of interest starts at character number 51 of the row i.e. cell number 51 and ends at 63.
The fifth column which is the third column of interest starts at character number 77 of the row i.e. cell number 77 and ends at 89.
I need to create NR separated text files and have the data of interest be written in the following format for each of the NR loop:
1) For each file the first 16 lines (rows) there is a similar text that is required to be at each file. For example :
"Thank you for your help!
I do appreciate it
and so on "
2) from Line 17 to Line 22 I need to print the data that has been read previously for r1 to r6 respectively, such a way that information of third column is being printed at character (cell) number 24, information of forth column is being printed at character (cell) number 40 and information of fifth column is being printed at character (cell) number 56.
3) for lines 17-22 I need to add four new columns at cells number , 4,8,12 and 16 respectively such that
A) the first column is 1 for r1, 2 for r2 and etc.
B) The second column is 1 for r1 and r2 and 2 for the r3 to r6
C) the third column is the same as second column
D) the forth column is always 0.
Wow! I know it might be so hard to get the point with the text :D
I hope you could help me with that !
So just to sum Up the points. I need NR separated files which names are from 1 to NR. Each of these NR files are related to the same loop in my large file.
Thanks!
All the best!

Accepted Answer

dpb
dpb on 6 May 2015
"... I need NR separated files which names are from 1 to NR..."
See the FAQ at How_can_I_process_a_sequence_of_files? for the naming issue.
Other than that, it looks like pretty straightforward application of a set of specific formatting strings using the appropriate width fields of the proper type for the data whether it be string, integer or floating point.
See
doc fprintf % for details and examples
It's not possible to decipher the precise format from the above description w/o way too much effort to try to parse the descriptions...an actual example (reduced in number of lines to the bare minimum; a repeat of two or three fields is just as illustrative of method as is 200+) would be much easier to follow than trying to parse the text above.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!