Effacer les filtres
Effacer les filtres

Strange issue encountered using textscan and cell arrays

1 vue (au cours des 30 derniers jours)
Peter Ryan
Peter Ryan le 14 Oct 2016
Commenté : Star Strider le 14 Oct 2016
When I run the following snippet of code:
str = '(3.3940269999999999,1,0) (0,3.3940269999999999,0) (0,0,2.024902)';
format = '(%f, %d, %d) (%d, %f, %d) (%d, %d, %f)';
A = textscan(str, format);
matrix = [A{1,1}, A{1,4}, A{1,7} ; A{1,2}, A{1,5}, A{1,8}; A{1,3}, A{1,6}, A{1,9}]
I get the following output matrix =
3 0 0
1 3 0
0 0 2
Why were the floating point numbers converted to integers?

Réponse acceptée

Star Strider
Star Strider le 14 Oct 2016
When I run a slightly modified version of your code (because format is a function that defines the format displayed in the Command Window and Tooltips), I get:
format_string = '(%f, %d, %d) (%d, %f, %d) (%d, %d, %f)';
A = textscan(str, format_string);
matrix =
3×3 int32 matrix
3 0 0
1 3 0
0 0 2
That is likely because of the '%d' format descriptors. Convert them to '%f':
format_string = '(%f, %f, %f) (%f, %f, %f) (%f, %f, %f)';
to get:
matrix =
3.394 0 0
1 3.394 0
0 0 2.0249
  3 commentaires
Peter Ryan
Peter Ryan le 14 Oct 2016
Thanks!
Star Strider
Star Strider le 14 Oct 2016
My pleasure!

Connectez-vous pour commenter.

Plus de réponses (1)

Eamon
Eamon le 14 Oct 2016
I think if you use the format function like below before the rest of your code, it will show the original floating point numbers.
format long
  1 commentaire
Eamon
Eamon le 14 Oct 2016
Nevermind, I tried this and it doesn't work.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion 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!

Translated by