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

19 ビュー (過去 30 日間)
Michael Boyle
Michael Boyle 2021 年 10 月 18 日
編集済み: Scott MacKenzie 2021 年 10 月 18 日
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

採用された回答

Scott MacKenzie
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 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by