Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

appending columns with fprintf

421 views
Skip to first unread message

Rachel Entwistle

unread,
Apr 27, 2010, 10:32:04 AM4/27/10
to
I'm reducing data and want to append the results, as columns, to an exisiting .txt file created using fprintf.

The intended layout is as such:
Column 1: All header names (13 rows)
Column 2+: row 1 (string), rows 2-13 (numbers)

I am having trouble figuring out how to append column 2 (and additional columns, later on) to the existing .txt file. fprintf will only append data after the last row of the headers column.

Code is below:
output = [number1; number2; number3];

if 0 == exist('Sample.txt', 'file');
headers = {'Name1'; 'Name2'; 'Name3'; 'Name 4'};
fid = fopen('Sample.txt', 'a');
for i = 1 : length(headers);
fprintf(fid, '%s\n', headers{i});
end
fclose(fid);
end

fid = fopen('Sample.txt', 'a');
fprintf(fid, '%s\n', String);
fprintf(fid, '%6.1f\n', output);
% fprintf(fid,
fclose(fid);

Is what I'm proposing even possible? I am not having trouble printing the headers column. That is working beautifully. Its the remaining columns that are giving me trouble.
The number of rows will always be constant, but the number of columns are unknown (I will be constantly adding new columns by opening (fopen) and closing (fclose) the file each time).

I have also considered using xlswrite, but I can't determine a way to append data. I also can't think of a simple way to find the number of columns and convert that number to a recognizable cell name in Excel (e.g. how to I tell matlab to understand that column 57 is equivalent to column BE in excel)?

Thanks in advance for your help.

Alan B

unread,
Apr 27, 2010, 11:30:22 AM4/27/10
to
"Rachel Entwistle" <rachel...@gmail.com> wrote in message <hr6sh4$mur$1...@fred.mathworks.com>...

http://www.mathworks.com/matlabcentral/fileexchange/15748-excel-column-number-to-column-name


But you should be able to accomplish what you want with fprintf, provided that you start thinking about the text file in terms of rows rather than columns. Why do you need to append columns? I don't think I've ever seen data stored like that in a text file, not if it was meant to be written or read by a program.

If you really need the data stored by column rather than by row, I'd think the easiest approach would be to write it all out into a Matlab cell array, or a row-based text file, read it back, transpose the array, and then rewrite it.

Alan B

unread,
Apr 27, 2010, 11:34:05 AM4/27/10
to

Walter Roberson

unread,
Apr 27, 2010, 11:39:39 AM4/27/10
to
Rachel Entwistle wrote:
> I'm reducing data and want to append the results, as columns, to an
> exisiting .txt file created using fprintf.

> Is what I'm proposing even possible?

No.

Okay, that's a bit of a white lie. Since you are strictly *increasing*
the size of the file, it _is_ possible, but it requires that you open
the file in 'r+' mode, read the entire contents of the file into memory,
fseek() back to the beginning of the file, and then write out the new
contents (old data with new columns appended) just as if you were
writing everything to a new file.

If you were attempting to shorten the total length of the file, then
that would not be possible with the facilities Matlab provides (but it
is theoretically possible on every operating system that Matlab supports
-- but only since XP SP2 has there been a uniform way of doing it at the
operating system level.)

In any case, altering the contents of the "middle" of a file is not
recommended unless you get into some advanced safety techniques: suppose
you were in the middle of an alteration and your program crashed or the
power failed or your disk developed a bad sector or the like? If you are
altering the file in place, you would be left with a partly altered file
and no untouched original version of the file that you could start over
with. For this reason, unless you have pretty much memorized the
relevant operating system standards clauses and know _exactly_ what you
are doing, it is recommended that you never modify a file "in place"
(except to append to it): that instead, you copy the contents you want
out of the original file and write the modified contents to a new file,
and only when you are sure that everything succeeded, rename the new
file to the old name.

0 new messages