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

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

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

As someone pointed out recently, for that range of values you can use textscan or readmatrix if you force the interpretation of columns as uint64
In that case, what will be the command to execute, can you please help
%y = readmatrix(filename, 'OutputType','uint64');
% uint64 can hold up to 20 decimal digits
intmax('uint64')
ans = uint64 18446744073709551615
https://www.mathworks.com/matlabcentral/answers/1602140-storing-many-digits-using-readtable#answer_846715
% Use textscan to read in as string
fileID = fopen(filename);
C = textscan(fileID,'%s %s','Delimiter',',');
fclose(fileID);
After typing help readmatrix, mine is showing readmatrix not found.
Any other alternative way,thanks
Use textscan as an alternative. There is lower level functions "fscanf" as well.
Actually I have two files 'A.txt' and 'B.txt'. A contains 2 columns both containing 19/20 digit integer. B contains single column containing 19/20 digit integer.
I am implementing the following code by cross-matching the B column and 1st column of A by;
(1) load A.txt
(2) load B.txt
(3) i=1;
while i<=size(B,1);
k=1;
while k<=size(A,1);
if B(i)==A(k,1);
B(i,2)=A(k,2);
end
k=k+1;
end
i=i+1;
end
Then I want to write the output B as a .txt file with no precision lost.
Can anyone please help to resolve this problem
You absolutely cannot use "load" for this purpose. It is hopeless to continue to try to use load().
We have been describing ways that can work. The easiest way will depend on your MATLAB version, which we asked you but you did not answer
Sir my matlab version is R2016a. I am facing the problem described above. It would be of great help to me if someone resolves this issue.Thanks
Sir my matlab version is 9.0.0.341360 (R2016a)
Yes sure, here are my two files 'A.txt' and 'B.txt' are in the zip file data. Sir please help me out.Thanks
@chunru ,sir could you please help me out from this problem, it's very much needed now,thanks.
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')
Thanks a lot sir, it worked, a great relief.

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!