Thread Subject: writing data and text in TXT file

Subject: writing data and text in TXT file

From: Hosein

Date: 23 May, 2008 18:34:02

Message: 1 of 8

I have 'for/end' code. In each time I want to write some
data and text in a data file (for example:'output.txt').In
each time the new data and text needs to be added to the
file and at the end of the code, I need the file
('output.txt') with data and text in all times. Any helps
will be appreciated.
I hope that I am clear enough. If not, let me know.



Subject: writing data and text in TXT file

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 23 May, 2008 18:45:22

Message: 2 of 8

In article <g172mq$2nm$1@fred.mathworks.com>,
Hosein <Kalaeimh@yahoo.com> wrote:
>I have 'for/end' code. In each time I want to write some
>data and text in a data file (for example:'output.txt').In
>each time the new data and text needs to be added to the
>file and at the end of the code, I need the file
>('output.txt') with data and text in all times. Any helps
>will be appreciated.
>I hope that I am clear enough. If not, let me know.

fid = fopen('output.txt','at');

for K = 1:1000
  fprintf(fid,'%s %d\n','We are now at iteration #', K);
end

fclose(fid);


Alternately, if there are reasons not to keep the file open for
the entire time, then you can do,

for K = 1:1000
  fid = fopen('output.txt','at');
  fprintf(fid,'%s %d\n','We are now at iteration #', K);
  fclose(fid);
end


See also the -append option of the save() command.
--
  "And that's the way it is." -- Walter Cronkite

Subject: writing data and text in TXT file

From: Hosein

Date: 23 May, 2008 19:38:01

Message: 3 of 8

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <g173c2$8j$1@canopus.cc.umanitoba.ca>...
> In article <g172mq$2nm$1@fred.mathworks.com>,
> Hosein <Kalaeimh@yahoo.com> wrote:
> >I have 'for/end' code. In each time I want to write some
> >data and text in a data file (for example:'output.txt').In
> >each time the new data and text needs to be added to the
> >file and at the end of the code, I need the file
> >('output.txt') with data and text in all times. Any helps
> >will be appreciated.
> >I hope that I am clear enough. If not, let me know.
>
> fid = fopen('output.txt','at');
>
> for K = 1:1000
> fprintf(fid,'%s %d\n','We are now at iteration #', K);
> end
>
> fclose(fid);
>
>
> Alternately, if there are reasons not to keep the file
open for
> the entire time, then you can do,
>
> for K = 1:1000
> fid = fopen('output.txt','at');
> fprintf(fid,'%s %d\n','We are now at iteration #', K);
> fclose(fid);
> end
>
>
> See also the -append option of the save() command.
> --
> "And that's the way it is." -- Walter
Cronkite



Thanks, but the numbers are not just one number every times.
There are matrices. for example I want to say :
"
Number of family in each rooms
3 4 5 6
...
...
Number of boys in each room
0 1 0 3
...

"

As you can see number of family is a matrix or number of
boys is a matrix.
I want to put a matrix after each statement.

Subject: writing data and text in TXT file

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 23 May, 2008 20:28:30

Message: 4 of 8

In article <g176ep$l8s$1@fred.mathworks.com>,
Hosein <Kalaeimh@yahoo.com> wrote:
>roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
>message <g173c2$8j$1@canopus.cc.umanitoba.ca>...
>> In article <g172mq$2nm$1@fred.mathworks.com>,
>> Hosein <Kalaeimh@yahoo.com> wrote:
>> >I have 'for/end' code. In each time I want to write some
>> >data and text in a data file (for example:'output.txt').In
>> >each time the new data and text needs to be added to the
>> >file and at the end of the code,

>> fid = fopen('output.txt','at');
>> for K = 1:1000
>> fprintf(fid,'%s %d\n','We are now at iteration #', K);
>> end
>> fclose(fid);

>Thanks, but the numbers are not just one number every times.
>There are matrices.

So write them out then. Whether you output a single line or
multiple lines does not matter for the basic task of appending to
a file.

I get the impression that when you talk about appending to a file,
you are not asking about how to add additional data on to the end
of a file that already exists (e.g., to log information from another
distinct run of a simulation): I get the impression that you are
asking a question about basic I/O to produce a single function.

For example, if all you knew about was save -ASCII then you
might have formed the impression that outputing to a file always
overwrites the file. But there are a number of other I/O calls
in Matlab that do not overwrite what is already there. And even
if all you knew was save -ASCII then you could always use
save -ASCII -APPEND


>for example I want to say :
>"
>Number of family in each rooms
>3 4 5 6
>...
>...
>Number of boys in each room
>0 1 0 3
>...
>
>"

maxrooms = 8;
maxhouses = 15;
medperroom = 3;
stdperroom = 1.5;
fid = fopen('output.txt','at');
for K = 1:20
  nrooms = 1 + floor(maxrooms * rand);
  nhouses = 1 + floor(maxhouses * rand);
  famperroom = abs( round(medperroom + randn(nhouses,nrooms) * stdperroom) );
  boyprob = 0.5 - (rand(nhouses,nrooms) - 0.5) .^ 3;
  boysperroom = round(boyprob .* famperroom);
  fmt = repmat('%d ',1,nrooms);
  fmt(end) = [];
  fprintf(fid, 'Number of family in each room\n');
  fprintf(fid, [fmt '\n'], famperroom .');
  fprintf(fid, 'Number of boys in in each room\n');
  fprintf(fid, [fmt '\n'], boysperroom .');
end
fclose(fid);
--
  "We may gather out of history a policy, by the comparison and
  application of other men's forepassed miseries with our own like
  errors and ill deservings." -- Sir Walter Raleigh

Subject: writing data and text in TXT file

From: arraval@gmail.com

Date: 23 Jun, 2008 20:53:47

Message: 5 of 8

Hello,

I have ,maybe, a similar problem. I am trying to write a really long
column vector to an output file using fprintf. The tricky part is that
the vector is being updated each iteration (an entire new vector is
calculated) of the for loop.
So what I have not been able to do is to write every new vector as a
new column into the output file keeping the old columns, and without
keeping memory of the old data in matlab because this is a time and
memory consuming calculation.

Thanks for your help.

arraval@gmail.com

Subject: writing data and text in TXT file

From: dpb

Date: 23 Jun, 2008 21:01:03

Message: 6 of 8

arraval@gmail.com wrote:
...
> So what I have not been able to do is to write every new vector as a
> new column into the output file keeping the old columns, and without
> keeping memory of the old data in matlab because this is a time and
> memory consuming calculation.
...

Well, sequential files are, well....sequential.

My first suggestion would be to simply accept that if the data are being
generated column-wise to write them in that fashion and then reshape the
resulting vector as desired.

Alternatively, treat the file as direct access as Walter outlined, but
you'll have to write each new line as a fixed length record in order to
be able to keep track of the location in the file.

All in all, since Matlab doesn't support fixed-record files natively,
the first solution is probably the simpler...

--

Subject: writing data and text in TXT file

From: arraval@gmail.com

Date: 23 Jun, 2008 22:36:27

Message: 7 of 8

On Jun 23, 4:01=A0pm, dpb <n...@non.net> wrote:
> arra...@gmail.com wrote:
>
> ...> So what I have not been able to do is to write every new vector as a
> > new column into the output file keeping the old columns, and without
> > keeping memory of the old data in matlab because this is a time and
> > memory consuming calculation.
>
> ...
>
> Well, sequential files are, well....sequential.
>
> My first suggestion would be to simply accept that if the data are being
> generated column-wise to write them in that fashion and then reshape the
> resulting vector as desired.
>
> Alternatively, treat the file as direct access as Walter outlined, but
> you'll have to write each new line as a fixed length record in order to
> be able to keep track of the location in the file.
>
> All in all, since Matlab doesn't support fixed-record files natively,
> the first solution is probably the simpler...
>
> --

I found the dlmwrite() ML function and now things have change a
little.

Now what I have not been able to do is to write every new
vector as a new column into row 0 of the output file.

I am using this instruction:
 for k =3D 1:N
  ...
  dlmwrite('output.txt', M, '-append', 'roffset',0,'coffset', k,
'delimiter', '\t');
  ...
 end

where M is a column vector that is recalculated every
iteration.


This is an example of what I am getting:

1
2
3
  4
  5
  6
    7
    8
    9

And this is what I want:

1 4 7
2 5 8
3 6 9

Thanks for your help.
I hope you have a suggestion to solve this.

Thanks for your previous response

Subject: writing data and text in TXT file

From: dpb

Date: 23 Jun, 2008 22:58:28

Message: 8 of 8

arraval@gmail.com wrote:
...
> I found the dlmwrite() ML function and now things have change a
> little.
>
> Now what I have not been able to do is to write every new
> vector as a new column into row 0 of the output file.
...

> I hope you have a suggestion to solve this.

My suggestion still stands...sequential files are, by their nature,
sequential.

If instead you had written

1
2
3
4
5
6
7
8
9

You could follow that w/

 >> fid=fopen('test.dat', 'rt');
 >> x = fscanf(fid,'%d', [3,inf])
x =
      1 4 7
      2 5 8
      3 6 9
 >> fid=fclose(fid);

Precisely what you would like...

IOW, solve the "problem" on the other end...

--

Tags for this Thread

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.

rssFeed for this Thread

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com