MATLAB >> Data manipulation in text file

by sagar poudel » Fri, 12 Mar 2010 12:59:07 GMT

Hi I have a text file as which I have to read and edit only a single or 2 columns and save it again with the same name.example

1 3 5 6 7
2 4 8 11 12
2 3 6 7 13
....................... It goes like this for hundreds of columns. I need to read 3rd column only and subtract 1 from all the elements in that columns such that only that column is changed in new text file. I tried the command like this..


file1='C:\a1.txt'
fileid=fopen(file1,'r')
A=fscanf(fileid,'%f')

(don't know the command here)

file2='C:\anil\data_40.3125_-105.9375'
ofid=fopen(file2,'w')
fprintf(ofid,'%6.2f %6.2f %6.2f %6.2f\n',A)

Just I don't know how to manipulate data for this file. Any help is appreciated.Its urgent too, I have thousands of file in the same folder with similar names.

The second part is: Please let me know if I can read the whole files in the folder at once. All files need the same manipulation. For files, only the extension is different. Lets say 101.234_200.352 and 102.223_203.445. I have seen somewhere '%6.4f_%6.4f'
to name these type of files. If it is not possible, only the first part would be ok.Thanks

Sagar

MATLAB >> Data manipulation in text file

by Ashish Uthama » Fri, 12 Mar 2010 22:15:47 GMT


On Thu, 11 Mar 2010 23:59:07 -0500, sagar poudel < XXXX@XXXXX.COM >



% Look at
http://matlabwiki.mathworks.com/MATLAB_FAQ #Can_I_read_a_text_file_with_a_certain_format.3F
% Add TEXTSCAN to the list in above.
% Then replace the above code lines with:
data=load(file1);
data(:,3)=data(:,3)-1;



fprintf(ofid, '%f %f %f %f %f\n',data')




http://matlabwiki.mathworks.com/MATLAB_FAQ #How_can_I_process_a_sequence_of_files.3F

MATLAB >> Data manipulation in text file

by Rune Allnor » Fri, 12 Mar 2010 23:59:24 GMT


> 2 gt;1 2
> 2 >6 13
> ....................... t goes like this for hundreds of columns. I need to read 3rd column only and subtract 1 from all the elements in that columns such that only that column is changed in new text file. I tried the command l>ke>this..
>
> file1='>:\a1.txt'
> fileid=fopen(>ile1,'r')
> A=fscanf(fi>ei>,'%f')
>
> (don't know the com>an> here)
>
> file2='C:\anil\data_40.3125_>105.9375'
> ofid=fopen(>ile2,'w')
> fprintf(ofid,'%6.2f %6.2f %6.2f %>.2>\n',A)
>
> Just I don't know how to manipulate data for this file. Any help is appreciated.Its urgent too, I have thousands of file in the same folder with simi>ar>names.
>
> The second part is: Please let me know if I can read the whole files in the folder at once. All files need the same manipulation. For files, only the extension is different. Lets say 101.234_200.352 and 102.223_203.445. I have seen somewhere '%6>4f_%6.4f'
> to name these type of files. If it is not possible, only the first part would be ok.Thanks

For the one file:

1) Read all the file into memory as an array,
2) Close the original file,
3) Do the the modification on the array in memory
4) Open file again with 'delete contents' option
5) Write modified array to file
6) Close file

To process multiple files, wrap the above into a
function that takes a file name as argument, use
the command DIR to find the file names in the folder,
and call the command you just made on each file.

Rune

Similar Threads

1. txt data manipulation - save data from one complex txt file into separate txt files - MATLAB

2. txt data manipulation - save data from one complex txt file into

Thanks a lot. Walter.

Your suggestion works pretty well. It gives me exactly what I want. 

But I just realized that in my real file, the number of lines (text not numbers) after each data block varies sometimes. So I need to find a way to use the textscan headerlines number more wisely. 


Another possible way, I am thinking is that

can I scan my file for '#s'
then skip a certain number of lines (which does not vary)
textscan, save the data that follows

and then scan for the next '#s" .....

Thanks.
Andy Zhu


Walter Roberson < XXXX@XXXXX.COM > wrote in message <huotpr$grs$ XXXX@XXXXX.COM >...
> andy zhu wrote:
> 
> > I need to save the data between the line "#L strech-h  H  K  L ..." and 
> > the line '#c' into separate files. This file below has two blocks of 
> > such data and therefore needs to be saved in to two txt files. The 
> > original data has lot more blocks. Thanks a lot.
> 
> textscan() with HeaderLines set to 20 and an appropriate numeric format. When 
> that fails because it has found the #C line, save the data and then enter a 
> loop of textscan() with HeaderLines 25 and the numeric format, saving the data 
> each time. That loop should check that data was actually read before saving, 
> of course.

3. convert mat files (struct) to ascii text files / save data as ascii text - MATLAB

4. simple manipulation of a text file

Dear All,

I want to do some simple manipulation with a simple text file , which
typically reads:

FIL D'AU CFC FACE 100 PART PERIOD
  21 0 0 0
 * 0.000000 0.000000 0.000000
 A 2.884000 0.000000 0.000000
 A 0.000000 2.884000 0.000000
...
 A 5.768000 -2.884000 0.000000
 A 0.000000 0.000000 4.078590
   8 20 9 19 1 21
 A 6
    -10.92 2.20 1.00
   1 0
   -6.93
   F
FIL D'AU CFC MOD MIN CELL + IMP
  37 0 0 1
 * 0.000000 0.000000 0.000000
 A 2.884000 0.000000 0.000000
 A 0.000000 2.884000 0.000000
...
 A 1.768000 -2.784000 0.000000
 A 1.442000 -4.326000 4.923290
 A 6
    -10.92 2.20 1.00
 B 6
    -10.92 2.20 0.60 1.10 0.40
FFFFFFFFFTFF
28
 0.0 2.884 9
 0.0 1.44 5
 0.0 0.00 1 0 0.02 0.25 15 0.0001
0.0000000000001
0.0000000000001
   F

From this file I want generate two text files which are part of the
original file.
File 1, which reads:

  21
FIL D'AU CFC FACE 100 PART PERIOD
 A 0.000000 0.000000 0.000000
 A 2.884000 0.000000 0.000000
 A 0.000000 2.884000 0.000000
...
 A 0.000000 0.000000 4.078590

which are the first 2+21 lines of the original but with line 2 and
line 1 interchanged, only keep the first number in line 2, and
replace all * with there subsequent lines starting alphabet. In this
case, only one *, its subsequent line is A.

File 2, which reads:

  37
FIL D'AU CFC MOD MIN CELL + IMP
 A 0.000000 0.000000 0.000000
 A 2.884000 0.000000 0.000000
 A 0.000000 2.884000 0.000000
...
 A 1.442000 -4.326000 4.923290

similar operation as file 1, just different part from the original
file. In this case, line line " 37 0 0 1" only 37 (the total
following line numbers of atom x, y, z positions) while "0 0 1"
is deleted.

Thank you for your time and help.

David

5. Save text string and data into a text file - MATLAB

6. Save text and numeric data into one text file

I would like to write a matrix to a text file from matlab.  In addition, I want to include both column and row headers to avoid confusion.  Is there anyway to do this without using fprintf to write each value separately?  Ideally, one could use fprintf to enter in the headers and dlmwrite or something similar to write the numerical values but the -append parameter in dlmwrite automatically inserts a new line.  

Thanks,
Mike  

7. data files manipulation help - MATLAB

8. read mixed text, data file and find corresponding data values

Hello, could you help take a look my trouble for reading 
mixed text, data file. Thanks.

I have many *.txt files (each file corresponding a 
condition) like:
--------------------------start
Text Line 1
---
Text line n

Coordinates
Point 1
X 9 Y 10 Z 8

Text Line 1
---
Text line n

Coordinates
Point 2
X 1 Y 10 Z 8

Force
point 1
Direction
X Y Z
units Newton 10 20 30
----------------------------end

The number of text lines 'n' for different *.txt files is 
not the dame. However, the relative positions between the 
numeric value and its name flag, eg, 10 20 30 and Force, 
is fixed in all files.


How I can get the corresponding coordinates for Point 1 (9 
10 8) and Point 2 (1 10 8) and the Force values 10 20 30
for Point 1 by reading this *.txt file using function like

Read *.txt
if string ==Coordinates
 if string_in_next_line==point 1
    if string=x,Coordinate(1,1)=9
       else if string = y,Coordinate(1,2)=10
       else string==z,Coordinate(1,2)=8
     end
  elseif string_in_next_line==point 2
-------------------similar for point 2
elseif string ==force
 if
 -------------similar for trying to get force values and 
corresponding point and direction infor.
end