Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Writing formatted text files when column number of the ouput is uncertain?

Subject: Writing formatted text files when column number of the ouput is uncertain?

From: Stephen

Date: 31 Jul, 2007 13:34:16

Message: 1 of 11

I want to export some outputs by writing a formatted text file. For example, if there are three columns in my output, I will code as follows.

fid=fopen(‘test.txt’,’w’)
fprintf=(fid, ‘%12.8f, %12.8f, %12.8f\r\n’, test’)
fclose(fid)

However, the number of the columns in the output is not certain. For example, the number of output depends on another variable called “NumOut”, e.g. NumOut=4, the output has four columns; NumOut=8, the output has eight columns;etc.

If that’s the case, how to make the code or the format flexible so that it would automatically adjust the format to accommodate different numbers of column in the output when writing a formatted text file?

Thanks,

Stephen

Subject: Re: Writing formatted text files when column number of the ouput is uncertain?

From: us

Date: 31 Jul, 2007 14:40:50

Message: 2 of 11

stephen:
<SNIP wants to re-invent the wheel...

> I want to export some outputs by writing a formatted text file...

a hint:

     help dlmwrite;

which will probably do exactly what you want...

us

Subject: Re: Writing formatted text files when column number of the ouput is uncertain?

From: Stephen

Date: 31 Jul, 2007 15:03:58

Message: 3 of 11

Thanks for your response. But the output is too big to be exported to spreadsheet, which I assume is what dlmwrite does, if I understand it correctly.

Thanks,
Stephen

"us " <us@neurol.unizh.ch> wrote in message <f8nhli$gok$1@fred.mathworks.com>...
> stephen:
> <SNIP wants to re-invent the wheel...
>
> > I want to export some outputs by writing a formatted text file...
>
> a hint:
>
> help dlmwrite;
>
> which will probably do exactly what you want...
>
> us

Subject: Re: Writing formatted text files when column number of the ouput is uncertain?

From: us

Date: 31 Jul, 2007 15:15:56

Message: 4 of 11

stephen:
<SNIP this reply is beyond me...

> But the output is too big to be exported to spreadsheet, which I assume is what dlmwrite does, if I understand it correctly...

well,
1) why ASSUME - did you even bother to READ the help?!
2) why not just TRY the function - did you ever consider?!

us

Subject: Re: Writing formatted text files when column number of the ouput is uncertain?

From: Stephen

Date: 31 Jul, 2007 15:33:16

Message: 5 of 11

That's what HELP says "dlmwrite(filename,M,delimiter,R,C) writes matrix A into an ASCII-format file, using delimiter to separate matrix elements. The data is written to the SPREADSHEET filename, starting at spreadsheet cell R and C, where R is the row offset and C is the column offset. R and C are zero based so that R=0, C=0 specifies the first value in the file, which is the upper left corner."

You mean the filename doesn't have to be the name of a spreadsheet?

So, if the name of the file I want to write to is test.txt and the matrix I want to export is test, is it the right way to code?
 
dlmwrite('test.txt',test,'\t');

I am getting an error message.

Thanks

"us " <us@neurol.unizh.ch> wrote in message <f8njnb$2ke$1@fred.mathworks.com>...
> stephen:
> <SNIP this reply is beyond me...
>
> > But the output is too big to be exported to spreadsheet, which I assume is what dlmwrite does, if I understand it correctly...
>
> well,
> 1) why ASSUME - did you even bother to READ the help?!
> 2) why not just TRY the function - did you ever consider?!
>
> us

Subject: Re: Writing formatted text files when column number of the ouput is uncertain?

From: us

Date: 31 Jul, 2007 15:43:18

Message: 6 of 11

stephen:
<SNIP version problem...

> That's what HELP says
"dlmwrite(filename,M,delimiter,R,C) writes matrix A into an ASCII-format file, using delimiter to separate matrix elements...

bad news: you seem to have an older ML version(?)...

in r2007a it says:

DLMWRITE Write ASCII delimited file.
    DLMWRITE('FILENAME',M) writes matrix M into
         FILENAME using ',' as the delimiter to
         separate matrix elements.
    DLMWRITE('FILENAME',M,'DLM') writes matrix M into
         FILENAME using the character DLM as the
         delimiter.
<...>

furthermore, this works as expected

     m=magic(10);
     dlmwrite('foo.txt',m,'\t');

what version do you use?
what error do you get?
 
us

Subject: Re: Writing formatted text files when column number of the ouput is uncertain?

From: Stephen

Date: 31 Jul, 2007 17:01:34

Message: 7 of 11

My version is 6.5.1.

The code is running OK now even though I have an older version. Don't know what went wrong before. However, the matrix dimension got changed.

For example, if the dimension of the matrix is 5*9, when exporting it using dlmwrite function, it no longer appears as a 5*9 matrix in the output file. In your example, the 10*10 matrix became one single line in the output. Any way to keep the orginal dimension of matrix in the output?

Thanks,
Stephen
 
"us " <us@neurol.unizh.ch> wrote in message <f8nlam$3tv$1@fred.mathworks.com>...
> stephen:
> <SNIP version problem...
>
> > That's what HELP says
> "dlmwrite(filename,M,delimiter,R,C) writes matrix A into an ASCII-format file, using delimiter to separate matrix elements...
>
> bad news: you seem to have an older ML version(?)...
>
> in r2007a it says:
>
> DLMWRITE Write ASCII delimited file.
> DLMWRITE('FILENAME',M) writes matrix M into
> FILENAME using ',' as the delimiter to
> separate matrix elements.
> DLMWRITE('FILENAME',M,'DLM') writes matrix M into
> FILENAME using the character DLM as the
> delimiter.
> <...>
>
> furthermore, this works as expected
>
> m=magic(10);
> dlmwrite('foo.txt',m,'\t');
>
> what version do you use?
> what error do you get?
>
> us

Subject: Re: Writing formatted text files when column number of the ouput is uncertain?

From: us

Date: 31 Jul, 2007 17:22:29

Message: 8 of 11

stephen :
<SNIP <dlmwrite> conundrum...

> In your example, the 10*10 matrix became one single line in the output...

this is how it looks here (r2007a)

     m=reshape(1:8,2,4);
     dlmwrite('foo.txt',m,',');
     m
     type foo.txt
%{
m =
     1 3 5 7
     2 4 6 8

1,3,5,7
2,4,6,8
%}

us

Subject: Re: Writing formatted text files when column number of the ouput is uncertain?

From: Stephen

Date: 31 Jul, 2007 17:44:11

Message: 9 of 11

The output can be read the way you described as long as the output size is small.

However, when the output is too big to be read either in excel or in matlab, you have to open the text file and read the output, which is kind of difficult as the dimension of the matrix is messed up in the text file.

Stephen
"us " <us@neurol.unizh.ch> wrote in message <f8nr4l$kb3$1@fred.mathworks.com>...
> stephen :
> <SNIP <dlmwrite> conundrum...
>
> > In your example, the 10*10 matrix became one single line in the output...
>
> this is how it looks here (r2007a)
>
> m=reshape(1:8,2,4);
> dlmwrite('foo.txt',m,',');
> m
> type foo.txt
> %{
> m =
> 1 3 5 7
> 2 4 6 8
>
> 1,3,5,7
> 2,4,6,8
> %}
>
> us

Subject: Re: Writing formatted text files when column number of the ouput

From: dpb

Date: 31 Jul, 2007 14:05:16

Message: 10 of 11

stephen wrote:
> I want to export some outputs by writing a formatted text file. For
example, if there are three columns in my output, I will code as follows.
>
> fid=fopen(‘test.txt’,’w’)
> fprintf=(fid, ‘%12.8f, %12.8f, %12.8f\r\n’, test’)
> fclose(fid)
>
> However, the number of the columns in the output is not certain. For
example, the number of output depends on another variable called
“NumOut”, e.g. NumOut=4, the output has four columns;
NumOut=8, the output has eight columns;etc.
>
> If that’s the case, how to make the code or the format flexible
>
so that it would automatically adjust the format to accommodate
different numbers of column in the output when writing a formatted text
file?
>

[Apologies for wrapping/quoting fouled owing to long lines in
original...I guess it's getting worked on... :)]

I read the exchange and as also am stuck in (apparently) the dark ages
of previous version, if dlmwrite() doesn't behave as required, best I
can think of would be to create a variable holding the required format
string "on the fly" and use it.

Something like the following could be wrapped in a function...

» NC = 3;
» x=randn(5,3)
x =
     0.1746 -0.1364 -0.8323
    -0.1867 0.1139 0.2944
     0.7258 1.0668 -1.3362
    -0.5883 0.0593 0.7143
     2.1832 -0.0956 1.6236
» s='%f ';
» f=s;for i=1:NC-1; f=cat(2,f,s);end, f=strcat(f, '\n');
» sprintf(f,x)
ans =
0.174639 -0.186709 0.725791
-0.588317 2.183186 -0.136396
0.113931 1.066768 0.059281
-0.095648 -0.832349 0.294411
-1.336182 0.714325 1.623562

»

You could include a delimiter instead of the space I used in the
substring and the format desired as well for added flexibility.

It's a poor man's substitute for the (sadly) lacking facility in C
printf for a repeat field for formatting functions a la Fortran.

Of course, if NC gets to be very large and the field width is sizable
you may run into system record length problems.

hth...(or I didn't miss the mark of the request too far :) )

--

Subject: Re: Writing formatted text files when column number of the ouput

From: Stephen

Date: 31 Jul, 2007 19:36:08

Message: 11 of 11

It works! Thanks.

Stephen

dpb <none@non.net> wrote in message <f8o195$j6f$1@aioe.org>...
> stephen wrote:
> > I want to export some outputs by writing a formatted text file. For
> example, if there are three columns in my output, I will code as follows.
> >
> > fid=fopen(‘test.txt’,’w’)
> > fprintf=(fid, ‘%12.8f, %12.8f, %12.8f\r\n’, test’)
> > fclose(fid)
> >
> > However, the number of the columns in the output is not certain. For
> example, the number of output depends on another variable called
> “NumOut”, e.g. NumOut=4, the output has four columns;
> NumOut=8, the output has eight columns;etc.
> >
> > If that’s the case, how to make the code or the format flexible
> >
> so that it would automatically adjust the format to accommodate
> different numbers of column in the output when writing a formatted text
> file?
> >
>
> [Apologies for wrapping/quoting fouled owing to long lines in
> original...I guess it's getting worked on... :)]
>
> I read the exchange and as also am stuck in (apparently) the dark ages
> of previous version, if dlmwrite() doesn't behave as required, best I
> can think of would be to create a variable holding the required format
> string "on the fly" and use it.
>
> Something like the following could be wrapped in a function...
>
> » NC = 3;
> » x=randn(5,3)
> x =
> 0.1746 -0.1364 -0.8323
> -0.1867 0.1139 0.2944
> 0.7258 1.0668 -1.3362
> -0.5883 0.0593 0.7143
> 2.1832 -0.0956 1.6236
> » s='%f ';
> » f=s;for i=1:NC-1; f=cat(2,f,s);end, f=strcat(f, '\n');
> » sprintf(f,x)
> ans =
> 0.174639 -0.186709 0.725791
> -0.588317 2.183186 -0.136396
> 0.113931 1.066768 0.059281
> -0.095648 -0.832349 0.294411
> -1.336182 0.714325 1.623562
>
> »
>
> You could include a delimiter instead of the space I used in the
> substring and the format desired as well for added flexibility.
>
> It's a poor man's substitute for the (sadly) lacking facility in C
> printf for a repeat field for formatting functions a la Fortran.
>
> Of course, if NC gets to be very large and the field width is sizable
> you may run into system record length problems.
>
> hth...(or I didn't miss the mark of the request too far :) )
>
> --
>

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
format us 31 Jul, 2007 11:01:16
ascii us 31 Jul, 2007 11:01:15
dlmwrite us 31 Jul, 2007 11:01:15
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

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.
Related Topics