Why matlab can not save complex numbers or matrixes in ascii format?

Discussion in 'MATLAB' started by Peng Yu, Jul 2, 2008.

  1. Peng Yu

    Peng Yu Guest


    Please see the code below. Why matlab does not save complex numbers or
    matrixes in ascii mode?

    Warning: Imaginary part of complex variable 'A' not saved to
    ASCII file.
    Peng Yu, Jul 2, 2008
    1. Advertisements

  2. Because there is no accepted ASCII format to signal that a component is

    There are *many* things that save -ascii does not handle. If you
    need any of those things handled in ASCII file format, then
    fopen() the output file, use appropriate fprintf() statements to
    write the data *exactly* the way you want, and fclose() the file.
    Walter Roberson, Jul 3, 2008
    1. Advertisements

  3. Peng Yu

    Titus Guest

    To add to Walters reply: there are some simple ways to save A. You might
    e.g. save first the real part and beneath the imaginary part:
    A = 1+i;
    B = [real(A) imag(A)];
    save B.txt B -ascii

    Titus, Jul 3, 2008
  4. No, that will not write the imaginary part "beneath" the real part.

    Let A be (say) 5 x 5 of imaginary. Then [real(A) imag(A)] is
    5 x 10 of real, and save doesn't know that that there should be
    any kind of internal boundary. save will write each of the entries
    out in single precision floating point format, putting however
    many across as fit onto a line with the width it uses, and then it
    continues on to put as many on the next line, and so on. save
    will not put in any kind of marker of the original column boundaries.
    If save put (say) 7 values onto a line, it would end up looking like

    R11 R12 R13 R14 R15 I11 I12
    I13 I14 I15 R21 R22 R23 R24
    R25 I21 I22 I23 I24 I25 R31

    and so on (or possibly it would write in the transpose order of that.)

    Because save -ascii does not put any variable names in the saved file,
    if you want to save first the real part and beneath the imaginary part

    AR = real(A); AI = imag(A);
    save B.txt AR AI -ascii

    This will write out all of the values from AR first (but again with
    no indicator of what the height or width were originally) and then
    will put a line break and then will write out the values from AI.

    As far as I am concerned, save -ascii is entirely useless
    for any serious use, and at best has value for "quick and dirty"
    Walter Roberson, Jul 3, 2008
  5. Peng Yu

    Titus Guest

    Hi Walter,

    are you sure (I guess you are), but this is entirely new to me. I'm really
    no friend of saving to ascii format, but when I had to dump a matrix of MxN
    to a text file using save -ascii, I always had a file with N lines and M
    numbers on each line. I just tried with

    A = rand(10) + i*rand(10);
    B = [real(A) imag(A)]
    save B.txt B -ascii
    clear B
    load B.txt -ascii
    A2 = complex(B:),1:end/2), B:),end/2+1:end));
    ans =

    which comes of course from the ascii dump with about 8 digits... So what's
    the problem? Or did I misunderstand your post completely?

    Titus, Jul 4, 2008
  6. Hi Titus,

    If you carefully read the walter's message above and then
    check your B.txt with any text editor you will find he is
    correct . It is saving in the form he wrote.

    Now comming to your code above. Since you are reading in
    same fashion as you stored so you are able to retreive the
    same matrix ( neglecting ascii dump approximations).
    I tried the code in my computer and is working but I am not
    too sure what will be the usage of storing in ascii in such
    format which could not be readable (meaningful) by other
    text editors.

    fprint could be more meaningfull in such cases.

    with reagrds,
    Nitin Chhabra, Jul 4, 2008
  7. Peng Yu

    Titus Guest

    Hi Nitin,

    thanks, maybe the word "beneath" was the problem? I wanted to say that first
    come the columns of real(A), then (I wanted to say "to the right", using
    "beneath" was my wrong english) the columns of imag(A). I could have stacked
    them just the same way (first the rows of real(A), then the rows of
    B = [real(A); imag(A)];
    save B.txt B -ascii

    But the shape of the matrix is (at least this is what I always thought and
    seen) indeed stored in the ascii file.

    BTW: you are right of course, if you want to write all entries e.g. in the
    form 2+i*3 or similar, then fprintf is what you need to use...

    Titus, Jul 4, 2008
  8. use...

    Some experiments....

    I couldn't find a format specifier that would recognise the
    imaginary part of a complex number - it was just dropped.
    A shame. Matlab is "like" C in many ways, why couldn't TMW
    have invented a new format specifier for complex numbers?

    I don't know if this is standard or not, but f90 (on my HP-
    UX box) dumps a complex array like this:


    write(*,*) x

    Output is:

    (1.0,1.0) (2.0,1.0) (1.0,2.0) (2.0,2.0)

    I don't know if it can read complex numbers back in in this
    format though.
    Steve Amphlett, Jul 4, 2008
  9. Peng Yu

    Titus Guest

    Hi Steve,
    reading the file should work like this:

    [re,im]=textread(filename, '(%f,%f)');
    A = reshape(complex(re, im), 2, 2)

    The only thing: you need to know the number of rows/columns...

    Titus, Jul 4, 2008
    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.