Clear Filters
Clear Filters

Problem using 'dlmwrite' into .txt file which contains 19/20 digit intergers

1 view (last 30 days)
I have a csv file (id.txt) which contains two columns having 19/20 digit integers.
I am executing the following commands in matlab workspace :-
(1) load id.txt
(2) dlmwrite('id_new.txt',id,'delimiter','\t','precision','%19i')
After executing the above commands, the last 3/4 digits of 2nd column in the file 'id_new.txt' gets changed, although the 1st column remains as it is. Can anyone please help me in sort out this problem.
The screenshots of two files id.txt and id_new.txt are attached herewith.

Accepted Answer

Chunru
Chunru on 10 Dec 2021
MATLAB double data type cannot hold so many digits in your imput data. It will round off the data to what-so-ever a double type can hold.
If you want to ensure that no information is lost, you can always read in the data as strings and then write the data as strings.
  17 Comments
Walter Roberson
Walter Roberson on 10 Dec 2021
fid = fopen('A.txt');
A_data = cell2mat(textscan(fid, '%u64,%u64', 'headerlines',1));
fclose(fid)
fid = fopen('B.txt');
B_data = cell2mat(textscan(fid, '%u64', 'headerlines',0));
fclose(fid)
dlmwrite('id_new.txt',A_data, 'delimiter', '\t', 'precision', '%u')

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!