How do I convert strings in a dataset to numbers?
조회 수: 19 (최근 30일)
이전 댓글 표시
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
답변 (2개)
the cyclist
2013년 8월 6일
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
Iain
2013년 8월 7일
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Type Conversion에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!