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
    Hi,

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

    -0.8736252526127E-9
    -0.1928287267367E-11
    0.28373737626278E-12
    etc etc.

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

    fid=fopen('filename');
    [b,count]=fscanf(fid,'%25f',[1,inf]);
    fclose(fid);

    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

    jamesrc

    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
    Hi,
    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:
    -9.822387897877512E-009
    -8.718508242883641E-009
    -7.412423106618656E-009
    -6.024806197201503E-009
    -4.681798286449183E-009
    -3.482646037397550E-009
    -2.479999539418316E-009
    -1.677888998136245E-009

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

    -0.000982238789788
    -0.000871850824288
    -0.000741242310662
    -0.000602480619720
    -0.000468179828645
    -0.000348264603740
    -0.000247999953942
    -0.000167788899814

    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

    jamesrc

    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 =

    3

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

    a =

    -9.822387897877512e-009
    -8.718508242883641e-009
    -7.412423106618656e-009
    -6.024806197201503e-009
    -4.681798286449183e-009
    -3.482646037397550e-009
    -2.479999539418316e-009
    -1.677888998136245e-009

    >> fclose all

    ans =

    0

    >>

    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 *

    -0.9822
    -0.8719
    -0.7412
    -0.6025
    -0.4682
    -0.3483
    -0.2480
    -0.1678

    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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Matlab problen with reading from files - need help
  1. Reading from file (Replies: 3)

Loading...