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

How to append new lines to a file using fopen, fprintf or fwrite?

1,580 views
Skip to first unread message

Daniel

unread,
May 18, 2009, 1:09:01 PM5/18/09
to
I'm currently working on a research project and I have thousands of files to read, format, and write to a new file. However, I'm stuck on the appending portion of Matlab - I read in a line at a time of the file (which contains hundreds upon hundreds of amino acid sequences, in an incorrect format for a prediction model that I am to use).

output_filename = 'example.txt';
fid = fopen('datafile.phy', 'rt');
if (fid == -1)
error(['The file cannot be opened!'])
end

while feof(fid) == 0
line = fgetl(fid); %grab a line from the file
x = line (1 : length(line)); %remove the unwanted characters from the sequence
x = strrep(x, '-', ''); %remove dashes and replace with no spaces^

%now write the line to the example file -- output_filename
test = fopen(output_filename, 'a');
write = fwrite(test, x, 'char*1'); %write the line to the file, which is opened for
%appending. Or for fprintf I've tried:
write = fprintf(output_filename, '\n', x);

end
fclose(test);
fclose(fid);
return


The problem is my data is all on 1 very long line. Instead of the 12 lines it should be, without spaces, and without dashes. Therefore it is not APPENDING the way I would like it - it is just simply attaching 1 line to the next, and removing the spaces and dashes. Any help would be greatly appreciated.

Thanks!
-Dan

Steven Lord

unread,
May 18, 2009, 1:11:18 PM5/18/09
to

"Daniel " <che...@indiana.edu> wrote in message
news:gus4nd$9hf$1...@fred.mathworks.com...

> I'm currently working on a research project and I have thousands of files
> to read, format, and write to a new file. However, I'm stuck on the
> appending portion of Matlab - I read in a line at a time of the file
> (which contains hundreds upon hundreds of amino acid sequences, in an
> incorrect format for a prediction model that I am to use).
>
> output_filename = 'example.txt';

> fid = fopen('datafile.phy', 'rt');

*snip*

> %now write the line to the example file -- output_filename
> test = fopen(output_filename, 'a');

You're reading from the input file as text; so why aren't you writing to the
output file as text as well?

*snip*

--
Steve Lord
sl...@mathworks.com


Daniel

unread,
May 18, 2009, 1:24:02 PM5/18/09
to
"Steven Lord" <sl...@mathworks.com> wrote in message <gus4rf$hkb$1...@fred.mathworks.com>...
I do realize that fwrite will not properly work, so i comment that portion out and try it with fprintf (which is supposed to be used with fopen(...., 'rt') -- however the same problem persists. Whether I'm writing text or not, the issue still remains.

Vadim Teverovsky

unread,
May 18, 2009, 2:31:50 PM5/18/09
to
your fprintf call should look like this
fprintf(fid2, '%s\n', x);


If you don't put in a format, it only prints out a new line.


"Daniel " <che...@indiana.edu> wrote in message

news:gus5ji$6gt$1...@fred.mathworks.com...

0 new messages