How to use precision only when needed with dlmwrite ?

조회 수: 19 (최근 30일)
Benjamin Lemmer
Benjamin Lemmer 2019년 2월 14일
편집: Stephen23 2019년 2월 17일
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:
20190212114854371;517585652892199;20190212114852000;-2.3511062;4.999093;5.3438582;0.28869548;0.16085984;-0.57739097;52.27151;10.533472
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:
2.019e+16;1.8509e+14;2.019e+16;-1.6664;6.3925;7.3933;0.0042612;0;0.0042612;52.274;10.527;0
which is not enough or it adds a lot of zeros, which slows down the writing process, if i use the needed precision:
20190211112837504.00000000;221480220621999.00000000;20190211112835000.00000000;-0.69032930;5.75713100;7.90648400;0.15009704;0.01083470;0.10411896;52.26511800;10.53640600;0.00000000
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.)
  댓글 수: 2
Stephen23
Stephen23 2019년 2월 16일
편집: Stephen23 2019년 2월 17일
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.
Benjamin Lemmer
Benjamin Lemmer 2019년 2월 16일
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 ?

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 2월 14일
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 .
  댓글 수: 4
Benjamin Lemmer
Benjamin Lemmer 2019년 2월 16일
Too bad, it actually did not work. Do you have another idea how i could reach my goal ?
Stephen23
Stephen23 2019년 2월 17일
편집: Stephen23 2019년 2월 17일
@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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by