How to prevent Matlab from rounding numbers when it saves them to a variable?
10 次查看(过去 30 天)
显示 更早的评论
I am trying to read numbers from a text file, post-process them in Matlab, and replace them in the text file. When I read in a number it automatically rounds it to fewer decimals so it doesn't match when I try to replace it in the text file.
text1 = '* -8.93201-5 .74967 -.734942';
var1_parts = cell2mat(textscan(text1(9:24),'%f'));
var1 = [var1_parts(1),var1_parts(2)];
var2 = [var1(1)*1.5,var1(2)];
var1_text = strcat(num2str(var1(1)),num2str(var1(2)));
var2_text = strcat(num2str(var2(1)),num2str(var2(2)));
text2 = strrep(text1,var1_text,var2_text);
Every step through the process Matlabs alters the number a little bit more from -8.93201-5 to -8.9320-5 to -8.932-5. I need it to be the original number in the variable field so that strrep() can find it. Does anyone know how to change this? Thanks
0 个评论
采纳的回答
Scott MacKenzie
2021-10-18
编辑:Scott MacKenzie
2021-10-18
The rounding you are observing occurs through the num2str function. You can control this using one of the other variants of num2str, for example
num2str(var1(1), 5); % 5 significant digits
num2str(var1(1), '%f'); % as per the format specification
0 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!