Filter löschen
Filter löschen

How do I convert strings in a dataset to numbers?

19 Ansichten (letzte 30 Tage)
Randy
Randy am 6 Aug. 2013
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 Kommentar
Jan
Jan am 7 Aug. 2013
An explicit example of the available input would be helpful.

Melden Sie sich an, um zu kommentieren.

Antworten (2)

the cyclist
the cyclist am 6 Aug. 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.

Iain
Iain am 7 Aug. 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"

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by