MATLAB Answers


How to use precision only when needed with dlmwrite ?

Asked by Benjamin Lemmer on 14 Feb 2019 at 12:08
Latest activity Edited by Stephen Cobeldick about 2 hours ago
I am having the problem that I want to save some data to a csv file.
The data I have in Matlab is something like this:
You see, it is mixed int and float values. I want to store them exactly like this. However when I use dlmwrite without precision I end up with data like this:
which is not enough or it adds a lot of zeros, which slows down the writing process, if i use the needed precision:
Is there a way to tell dlmwrite to onyl use precision if needed or is there another option to write csv data with precision ?
(Please do not be confused: I picked up different lines from the output.)


These data look much like timestamps 20190212114854371 20190212114852000. Of course dates/times should not be combined into one decimal integer (doing so is a total misrepresentation of the bases involved with dates and times). Dates and times should be stored in a datetime variable (or even a string/char variable), which would make it clearer how to then save it properly.
This is correct. It turned out that importing only numbers is much faster instead of the generated import routine by matlab.
Therefor and since I do not need to work with the time stamps I decided to use numbers. Is there a way to convert the datestamp after having importet it or a faster method to import the data than the generated script by matlab ?

Sign in to comment.




1 Answer

Answer by Walter Roberson
on 14 Feb 2019 at 12:24

No. dlmwrite only accepts one precision argument . That argument must be an integer (which is used as number of decimal places) or it must be ann fprintf format item describing aa single output value . It is valid to pass something like '%.12g' but there is no way to pass something conditional that says to use different format descriptions depending upon the value to be output (or the column number)
Furthermore inside matlab the values are not what you claim they are . There is no way to represent 0.1 exactly on floating point , only integer multiples of 2^(-N) for some integer N. Every one of the values you show with a decimal point has additional digits that you have not shown and matlab has no way of knowing whether those are wanted digits or not .


Show 1 older comment
That isn't the main problem. Look more closely at your data
That is 8 significant digits, then 7, then 8, then 8, then 8, then 8, then 7, then 8. Number of digits after the decimal place is 7, 6, 7, 8, 8, 8, 5, 5.
You are inconsistent on what you output. However, what you output for the floating point numbers is consistent with what %.8g would output.
If you do create a table with a mix of int64 and double, then Yes, writetable will write the int64 in integer form. However, when you use writetable, you lose control over the number of digits of output you use. It might accidentally give you what you want in this test case, but you cannot count on it.
Too bad, it actually did not work. Do you have another idea how i could reach my goal ?
@Benjamin Lemmer: write your own file-writing function based on fopen, fprintf, and fclose.
If you import the timestamps as character than you avoid any loss of their data, and can easily print these to the output file directly using the %s format operator.

Sign in to comment.