How to prevent Matlab from rounding numbers when it saves them to a variable?
조회 수: 17 (최근 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!