Matlab problen with reading from files -

In summary, reading the data in MATLAB using %e or 'format long' command results in wrong values. Use %e to read in scientific notation.
  • #1
confi999
19
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
  • #2
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.
 
  • #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.
 
  • #4
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).
 
  • #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
 

1. How do I read a file in Matlab?

To read a file in Matlab, you can use the fopen function to open the file, and then use the fscanf function to read the data from the file. Alternatively, you can use the importdata function to read the file into a data structure.

2. Why am I getting an error when trying to read from a file in Matlab?

There could be several reasons for this error. Some common reasons include incorrect file path or name, file permissions, and incorrect file format. Make sure that the file exists in the specified location, and that you have read permissions for the file. Also, make sure that the file is in a format that Matlab can read, such as a text file or a CSV file.

3. How can I read specific columns or rows from a file in Matlab?

You can use the textscan function to read specific columns or rows from a file. This function allows you to specify the format of the data and which columns or rows to read. You can also use the csvread function to read specific columns or rows from a CSV file.

4. Can I read from multiple files at once in Matlab?

Yes, you can use a loop to read from multiple files in Matlab. Within the loop, you can use the functions mentioned above to read the data from each file and store it in a data structure or perform any desired operations.

5. Is there a limit to the size of the file that can be read in Matlab?

Yes, there is a limit to the size of the file that can be read in Matlab. The limit depends on your system's memory and the version of Matlab you are using. For large files, it is recommended to use the memmapfile function, which allows you to read a file in chunks and conserve memory.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
5K
  • Programming and Computer Science
Replies
4
Views
711
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
940
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
783
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top