How to maintain the precision of data?

I am working with data in the range of microns and although I can read it from the text file with precision, when I work with the data in Matlab, the data isn't as precise as anymore. For example:
I start with data like : -432.731805 612.208233 1234.20203
I am using textscan to read it and then converting it into matrix using cell2mat.
The resulting calculations degrade in precision. I get output as:
648.000000 1314.000000
This is meaningless to me. Could anybody tell me how to retain the precision?
Thanks a lot.

12 Comments

the cyclist
the cyclist on 25 Jul 2013
Edited: the cyclist on 25 Jul 2013
Can you give the textscan formatting details? My guess is that you are explicitly reading in the data as integers, rather than as floating point numbers.
Ideally, give us a small example of the data, and the smallest part of the code that will show the problem, so that we can replicate it ourselves.
fid = fopen(Filename,'r');
data = textscan(fid, '%4.6f %4.6f %4.6f');
data1 = cell2mat(data);
fclose(fid);
I am also transferring this data collected to another function.
1. use %f instead of %4.6f
2. set your matlab's display format. for example type: "format long"
Great!! It worked. Thanks. Its second time in the day!! :D Also, would you recommend some link that can help me format the way I write data into a text file? Even if I have a '\n' in my fprintf command. It doesn't seem to be going to a new line to write the data. Thanks.
happy to hear that. you can read the documentation on the function you're using, and follow the links. for example:
Hey! It seems '\n' doesn't work in my program. When I try to write the data in a text file. All the data comes together instead of next set of data starting at another line. What might be going wrong?
@nl2605: How did you check that \n does not work? Notice that almost all editors can handle this linebreak correctly except for the Windows Editor. Using WordPad, MS Word, the Matlab editor, vi, Emacs, XEmacs, NotePad++ etc. let the line breaks appear correctly. So it is the MS Editor, which fails, but your program works correctly.
Hi..I am using an older version of Notepad..Is it that what is the problem?
No, nl2605, all versions of Notepad are concerned. If did not test this under Windows 8 because I'm in fear, that it still fails and I loose my confidence to MS.
I didn' get you. Is it Notepad or is it the program? I am using Windows 7.
dpb
dpb on 30 Jul 2013
Edited: dpb on 30 Jul 2013
It's both... :)
Under Windows, the newline character is actually two characters \r\l whereas on unix-like OS it is simply one. Once upon a time virtually all Windows programs expected the two-character sequence and it was a very big pain in the proverbial appendage to transfer files owing to the difference.
One of the prime symptoms of not using the two on Windows is that of the "run on" of lines in editors, etc., that you're seeing if those programs were written w/ the expectation that all files would have the Windoes-defined 2-character sequence. Notepad is one of those.
With time, programs began to take care of the problem behind the scenes as does Matlab internally on input or in its builtin editor. Notepad is about the only widely used text editor left on mass distributions that still has the old-timey behavior.
You can solve the problem one of two basic ways...
a) Use something other than Notepad--generally the recommended approach; there are many options; Wordpad would be one.
b) If you think you must continue w/ Notepad, then when you write files that are going to be used with it, write them with the proper linefeed for the OS. To do this most easily in/with Matlab use the 't' option on the fopen statement.
fid=fopen('yourtextfile', 'wt');
See
doc fopen
for more details.
Crazy!!!! Thanks a lot! :)

Sign in to comment.

Answers (0)

Categories

Asked:

on 25 Jul 2013

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!