Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab problen with reading from files - need help

  1. Aug 1, 2009 #1

    I have a formatted data file (obtained from fortran) which has 1 column and 24000 rows. The values are like

    etc etc.

    I have tried to read these data in matlab using the following command :


    I have tried with %g as well as the format specifier. Then I have also tried to use 'load' command of matlab. In all the cases I end up with wrong values of the data in the vector. I get somethiing like 0.0001, -0.0005, 0.003, etc .... These are far larger values compared to those in the source file.

    Can anyone advise me - how I can read those data successfully in matlab.
    Thank you.
  2. jcsd
  3. Aug 1, 2009 #2


    User Avatar
    Science Advisor
    Gold Member

    Use %e to read in scientific notation.

    You don't need to specify the field width and with the way you are using the size argument, it will work without it.
  4. Aug 3, 2009 #3
    Thank you. Using %e I did not get any different outcome.

    Suggested by someone I have used 'format long' command before those commands mentioned in my original post. This gave following improvement but still couldnot read data perfectly.

    Some of the data from my data file are like:

    and after reading (using 'long format' command) matlab is showing these as


    So all the data seem to be read as *******E-04 instead of *******E- 09

    Can anyone help me overcome this. Thank you very much.
  5. Aug 3, 2009 #4


    User Avatar
    Science Advisor
    Gold Member

    Hmm... not sure what's going on. Here's what I did:

    - I saved the data you just printed out as a file called 'read.txt'.
    - here, I've copied and pasted from my command window:

    >> fid = fopen('read.txt')

    fid =


    >> a = fscanf(fid,'%e')

    a =


    >> fclose all

    ans =



    If you do the same thing on your system, do you still get the wrong values? What are your display preferences set to? I mean, if I set my preferences to display short, a will look like this:

    a =

    1.0e-008 *


    The values are still correct, but there's a multiplier at the very top of the column vector (that you may not notice if you aren't looking for it).
  6. Aug 3, 2009 #5
    That was extremely helpful. Thank you.

    As my file has 24000 data I could never see that '1.0 e -008 *' part on the top.
    I made my file shorter and found it there.

    Now after using 'format long eng' command before everything I got the data in Matlab that looks like in the file. Thank you so much
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook