add new column into existing text file
Show older comments
i have 10 file and every file have 1 column data..i want to extract the column from all file and write into first file ..so, finally, in the first file, i will have ten column data..i try use load and fprint..i can load the column but when i try to write into file, the data is continues..not became as column..so, can someone help me??
Answers (1)
Walter Roberson
on 12 Dec 2011
You cannot add a column to a text file by opening it with 'a' (append) access. You must re-write the entire text file to have everything it had before as well as the new column.
However, if all of the data files have the same number of columns, it is easier to read all the data in first and then to write it out in one step.
[Code revised to make it much more robust]
magiclen = 6625;
magiccol = 5;
n ='average.txt';
files = dir('*.txt');
%the file average.txt might have been detected by dir() so remove it
tf = arrayfun(@(K) strcmp(n, files(K).name), 1:length(files));
files(tf) = [];
nfile = length(files);
if nfile == 0
error('There were no files found');
end
if nfile == 1;
warning(sprintf('Only one file found, only one column of output expected: %s', files(1).name);
end
Lat = zeros(magiclen, nfile)
for F = 1:nfile
thisfile = files(F).name;
try
f = load(thisfile);
catch me
%if there was a load problem, say so
error(sprintf('Failed loading file %s', thisfile));
end
if any(size(f) < [magiclen,magiccol])
error(sprintf('File %s loaded but is too small, only (%d by %d) but need (%d+ by %d+)', thisfile, size(f,1), size(f,2), magiclen, magiccol));
end
Lat(1:magiclen,F) = f(1:magiclen, magiccol);
end
fmt = ['\n' repmat('%15.6f', 1, nfile) '\n'];
fid = fopen(n, 'wt')
fprintf(fid, fmt, Lat.' ); %the .' is important!
fclose(fid);
Note the complete lack of a loop in writing out the data after it has all been read in.
Computing the format and using .' on the array to write out are tricks of the trade -- things that are not obvious until you have worked with MATLAB enough that suddenly they are obvious.
7 Comments
joo tan
on 13 Dec 2011
Walter Roberson
on 13 Dec 2011
The code I show extracts out column 5 from each input file.
Please put a breakpoint in at the fprintf() line, and report back on the values of
nfile
size(Lat)
fmt
size(f)
Also, I made some changes to the code to make it more robust. (Or I will have made them in a few minutes)
Walter Roberson
on 13 Dec 2011
Changes are in place.
joo tan
on 13 Dec 2011
Jan
on 13 Dec 2011
This is a typo. If you take a short look in Walter's program, you find, that the variable is called "nfile", not "nfiles". You see, that even Walter is not a programming machine but a human. It would be nice, if you try to fix such obvious mistakes by yourself.
I had the trailing-s typo too often in my code and decided to use the singular form for *all* variables. Because "file" is not a nice name for a list of files, I use "FileList" and "nFile", or "FileC" if it matters, that the list is a CELL.
Walter Roberson
on 13 Dec 2011
Sorry, yes, it was a typo, now corrected.
Mario
on 8 May 2013
Excellent, this code fits perfectly. Thanks Walter!
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!