Problem using 'dlmwrite' into .txt file which contains 19/20 digit intergers
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
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
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
on 10 Dec 2021
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
Apashanka Das
on 10 Dec 2021
In that case, what will be the command to execute, can you please help
Chunru
on 10 Dec 2021
%y = readmatrix(filename, 'OutputType','uint64');
% uint64 can hold up to 20 decimal digits
intmax('uint64')
ans = uint64
18446744073709551615
Walter Roberson
on 10 Dec 2021
https://www.mathworks.com/matlabcentral/answers/1602140-storing-many-digits-using-readtable#answer_846715
Chunru
on 10 Dec 2021
% Use textscan to read in as string
fileID = fopen(filename);
C = textscan(fileID,'%s %s','Delimiter',',');
fclose(fileID);
Apashanka Das
on 10 Dec 2021
After typing help readmatrix, mine is showing readmatrix not found.
Any other alternative way,thanks
Walter Roberson
on 10 Dec 2021
Which MATLAB version are you using?
Chunru
on 10 Dec 2021
Use textscan as an alternative. There is lower level functions "fscanf" as well.
Apashanka Das
on 10 Dec 2021
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
Walter Roberson
on 10 Dec 2021
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
Apashanka Das
on 10 Dec 2021
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
Apashanka Das
on 10 Dec 2021
Sir my matlab version is 9.0.0.341360 (R2016a)
Chunru
on 10 Dec 2021
Can you upload your data?
Apashanka Das
on 10 Dec 2021
Yes sure, here are my two files 'A.txt' and 'B.txt' are in the zip file data. Sir please help me out.Thanks
Apashanka Das
on 10 Dec 2021
@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')
Apashanka Das
on 11 Dec 2021
Thanks a lot sir, it worked, a great relief.
More Answers (0)
Categories
Find more on Text Files in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)