How to prevent Matlab from rounding numbers when it saves them to a variable?

24 Ansichten (letzte 30 Tage)
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

Akzeptierte Antwort

Scott MacKenzie
Scott MacKenzie am 18 Okt. 2021
Bearbeitet: Scott MacKenzie am 18 Okt. 2021
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

Weitere Antworten (0)

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by