Conversion to double from cell is not possible.

299 vues (au cours des 30 derniers jours)
Fabian Moreno
Fabian Moreno le 8 Juil 2020
Commenté : Adam Danz le 10 Juil 2020
Hi there, I am new in MATLAB and I have this problem "Conversion to double from cell is not possible", I uploaded a matrix with readtable and I delimited with (;). Then It made a matrix of 10200x3, in the third column I have values with "comma" decimal (25,7). I used str2double to change from cell array to double array, but the new value is (257). I would like to get the same value (25,7). Somebody could help me please.
  2 commentaires
madhan ravi
madhan ravi le 8 Juil 2020
Mind sharing a screenshot/file of that table ?
Fabian Moreno
Fabian Moreno le 8 Juil 2020
Hello Madhan, I don't mind here I attach the file and the screenshot. Thanks

Connectez-vous pour commenter.

Réponse acceptée

Adam Danz
Adam Danz le 8 Juil 2020
Modifié(e) : Adam Danz le 8 Juil 2020
There might be an option in readtable() or within the opts input that can specify what decimal character to use when reading in the data. I looked briefly but couldn't find what I'm imagining but I do recall that such an option exists. That would be the best solution, if possible.
To fix the data described in your question, first replace the commas in column 3 with periods.
% "C" is your cell array
C(:,3) = strrep(C(:,3),',','.');
then proceed with str2double.
d = str2double(C(:,3));
For tables
T.Var3 = strrep(T.Var3,',','.');
T.Var3 = str2double(T.Var3);
  3 commentaires
Fabian Moreno
Fabian Moreno le 8 Juil 2020
Hi Adam, I did it. Thank you a lot :)
Adam Danz
Adam Danz le 8 Juil 2020
That's because your data is not a cell array. It's a table.
I've updated my answer.

Connectez-vous pour commenter.

Plus de réponses (1)

Fabian Moreno
Fabian Moreno le 8 Juil 2020
I have one more question, when I Convert strings to double precision values, the result is a number like this (23.700). I'm trying to take out the extra zeros with (round). But the zeros continue there. Any suggestion to eliminate the zeros?.
  6 commentaires
madhan ravi
madhan ravi le 9 Juil 2020
xD
Adam Danz
Adam Danz le 10 Juil 2020
Glad I could help! :)

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