How to prevent Matlab from rounding numbers when it saves them to a variable?
15 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Michael Boyle
il 18 Ott 2021
Modificato: Scott MacKenzie
il 18 Ott 2021
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 Commenti
Risposta accettata
Scott MacKenzie
il 18 Ott 2021
Modificato: Scott MacKenzie
il 18 Ott 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
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Text Files in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!