How do I convert strings in a dataset to numbers?
19 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I have data that I imported from a csv file. The file was very large, so I did not want to go through and manually change all of the variables that Matlab recognized as text to numbers, but I need to do calculations with these values, so I do want them stored as doubles. I would be happy to have these data either in a dataset format with doubles or as a bunch of column vectors with doubles, but I do not know how to get either.
If I import the data as vectors, many of them say they are type "cell", and I cannot figure out how to make these numeric vectors (let alone do them all at once--since typing the code to convert all 100,000 variables by name would be sufficient work to make me abandon my dissertation).
If I import the data as a dataset, then many of the data seem to show up as strings (for instance the number of Quaker people in a particular county shows up as '0.00').
This is complicated by the fact that there is one column that I want left as a string. If this is too much to ask, I can lose it--I have numeric codes for these strings that I can reference.
1 commentaire
Réponses (2)
the cyclist
le 6 Août 2013
There are many conversion functions available. It would be helpful if you posted some of the data as an example. I think cell2mat() is likely to be useful. See
>> doc cell2mat
for details.
0 commentaires
Iain
le 7 Août 2013
Its a cheat, but provided your csv file has less than 256 columns and 65536 rows, you can import the whole lot with "xlsread"
[numbers, texts, raw] = xlsread('D:\data.csv');
numbers is a numeric array, with everything in doubles. - NaN where its not a number. texts is a cell array containing all of the text. raw is a cell array containing everything.
numbers(:,1) is the first column with numbers in it, starting at the first row with numbers in it... raw{4,3} is "C4"
0 commentaires
Voir également
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!