Thread Subject: Data manipulation in text file

Subject: Data manipulation in text file

From: sagar poudel

Date: 12 Mar, 2010 04:59:07

Message: 1 of 4

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

Subject: Data manipulation in text file

From: Ashish Uthama

Date: 12 Mar, 2010 14:15:47

Message: 2 of 4

On Thu, 11 Mar 2010 23:59:07 -0500, sagar poudel <acharya_anil@yahoo.com>
wrote:

> 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')

% 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;

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


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


>
> 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


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

Subject: Data manipulation in text file

From: dpb

Date: 12 Mar, 2010 14:17:07

Message: 3 of 4

sagar poudel wrote:
> 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.

If the contents of the new file are to include the data other than that
in the third column that have been modified, you'll have to rewrite the
whole file after making the modification. It would have to be a direct
access file, not sequential to do otherwise (w/o excessive gyrations,
anyway).

> ... I tried the command like this..
>
> file1='C:\a1.txt'
> fileid=fopen(file1,'r')
> A=fscanf(fileid,'%f')
>
> (don't know the command here)

A(:,3) = A(:,3)-1; % that was easy wasn't it? :)

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

If you have various numbers of fields, you'll find

repmat()

of inestimable value in building a variable formatting string instead of
writing out each field separately...

...

> The second part is: Please let me know if I can read the whole files in
> the folder at once. ...

Process each file individually, but you can automate this, yes.

doc dir

--

Subject: Data manipulation in text file

From: Rune Allnor

Date: 12 Mar, 2010 15:59:24

Message: 4 of 4

On 12 Mar, 05:59, "sagar poudel" <acharya_a...@yahoo.com> wrote:
> 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

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

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
data manipulati... sagar poudel 12 Mar, 2010 00:04:06
rssFeed for this Thread

Contact us at files@mathworks.com