Writematrix loses precision with string present in data
8 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Martin
le 18 Mar 2025
Réponse apportée : Walter Roberson
le 18 Mar 2025
I am saving a matrix to file and need full precision in the data. I am using the writematrix function, and my problem can be shown in this minimal example:
writematrix([1;0.123456789],"./testA.txt")
writematrix(["Test";0.123456789],"./testB.txt")
Then testA.txt:
1
0.123456789
And testB.txt
Test
0.12346
Is there a way to have strings (such as a header row) in my matrix but retain the full precision in the data?
0 commentaires
Réponse acceptée
Walter Roberson
le 18 Mar 2025
writematrix(["Test";compose("%.999g", 0.123456789)],"./testB.txt")
dbtype testB.txt
Retains full precision.
0 commentaires
Plus de réponses (1)
dpb
le 18 Mar 2025
Déplacé(e) : dpb
le 18 Mar 2025
["Test";0.123456789]
The problem, if there is one, is in combining the string and the numeric into one array; MATLAB can have only one data type in an array other than a cell array and so the numeric value was coerced to a string before it was written; ergo, it isn't writematrix that is the issue here, it's trying put a square peg in a round hole; mixing data types inappropriately.
Probably the easiest workaround with the minimal example would to use a table instead...
tT=table(0.123456789,'VariableNames',"Test")
writetable(tT,"testB.txt",'WriteVariableNames',1)
type testB.txt
0 commentaires
Voir également
Catégories
En savoir plus sur Text Files dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!