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

Discussion in 'MATLAB' started by stephen, Jul 31, 2007.

  1. stephen

    stephen Guest

    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
     
    stephen, Jul 31, 2007
    #1
    1. Advertisements

  2. stephen

    us Guest

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

    help dlmwrite;

    which will probably do exactly what you want...

    us
     
    us, Jul 31, 2007
    #2
    1. Advertisements

  3. stephen

    stephen Guest

    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
     
    stephen, Jul 31, 2007
    #3
  4. stephen

    us Guest

    stephen:
    <SNIP this reply is beyond me...
    well,
    1) why ASSUME - did you even bother to READ the help?!
    2) why not just TRY the function - did you ever consider?!

    us
     
    us, Jul 31, 2007
    #4
  5. stephen

    stephen Guest

    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
     
    stephen, Jul 31, 2007
    #5
  6. stephen

    us Guest

    stephen:
    <SNIP version problem...
    "dlmwrite(filename,M,delimiter,R,C) writes matrix A into an ASCII-format file, using delimiter to separate matrix elements...

    bad 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
     
    us, Jul 31, 2007
    #6
  7. stephen

    stephen Guest

    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
     
    stephen, Jul 31, 2007
    #7
  8. stephen

    us Guest

    stephen :
    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
     
    us, Jul 31, 2007
    #8
  9. stephen

    stephen Guest

    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
     
    stephen, Jul 31, 2007
    #9
  10. stephen

    dpb Guest

    example, if there are three columns in my output, I will code as follows.
    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.
    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 :) )

    --
     
    dpb, Jul 31, 2007
    #10
  11. stephen

    stephen Guest

    It works! Thanks.

    Stephen

     
    stephen, Jul 31, 2007
    #11
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.