Matlab problen with reading from files -

  • Context: MATLAB 
  • Thread starter Thread starter confi999
  • Start date Start date
  • Tags Tags
    files Matlab Reading
Click For Summary

Discussion Overview

The discussion revolves around issues related to reading formatted data files in MATLAB, specifically files containing scientific notation values. Participants explore various methods and commands to correctly import and display the data without discrepancies.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to read a data file with scientific notation using the fscanf function, but reports incorrect values in the resulting vector.
  • Another participant suggests using the %e format specifier for reading scientific notation and mentions that specifying field width is unnecessary.
  • A participant reports that using %e did not resolve the issue and notes that using the 'format long' command improved the output but still resulted in incorrect scaling of the data.
  • One participant shares their successful experience reading similar data and questions the original poster about their display preferences, suggesting that the issue may be related to how MATLAB displays the data rather than the data itself.
  • A later reply indicates that the original poster discovered the correct scaling factor after shortening their data file and found that using 'format long eng' resolved their issue.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the initial cause of the reading issue, but there is agreement that display settings and format commands can significantly affect the output in MATLAB.

Contextual Notes

Some participants mention specific commands and formats without fully resolving the underlying issues related to data interpretation and display preferences in MATLAB.

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

  • · Replies 8 ·
Replies
8
Views
3K
Replies
5
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 18 ·
Replies
18
Views
6K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 5 ·
Replies
5
Views
9K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
Replies
1
Views
5K
  • · Replies 2 ·
Replies
2
Views
5K