MATLAB Matlab problen with reading from files -

AI Thread Summary
The discussion revolves around reading a formatted data file in MATLAB that contains scientific notation values. The user initially faced issues with incorrect data representation after using the fscanf function, resulting in values that were significantly larger than expected. Suggestions included using the format specifier %e and removing the field width specification, but these did not resolve the issue. The user later discovered that MATLAB's display preferences were affecting how the data was presented, specifically that values were being displayed with a multiplier (e.g., 1.0e-008). By adjusting the display settings and using the 'format long eng' command, the user successfully read the data correctly, matching the original file values. The discussion highlights the importance of understanding MATLAB's display settings and format specifiers when handling scientific data.
confi999
Messages
18
Reaction score
0
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.
 
Physics news on Phys.org
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.
 
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.
 
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).
 
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
 

Similar threads

Back
Top