How to convert a column in matrix from string to numeric
Show older comments
I am having a trouble of converting a string column in a table to a numeric type. I have a table imported from .csv file containing 6 columns with headers and 4 of them should be numeric and one is date in format yyyyMMddhhmm, delimitator is ','.
When I load it using readtable, it assumes that everything is a string and I can't convert the 4 columns to a numeric type. I have tried to convert it through many something2somethingelse converters, did not work.
I tried even:
data = readtable(['data' cell2mat(indexVars(i))], 'Format','%s%f%{yyyyMMddhhmm}D%f%f%f');
It returned:
Error using readtable (line 216)
Unable to read the entire file. You may need to specify a different format, delimiter, or number of header
lines.
Note: readtable detected the following parameters:
'Delimiter', ',', 'HeaderLines', 0, 'ReadVariableNames', true
Any ideas?
3 Comments
Walter Roberson
on 13 Mar 2020
Can you attach part of the csv file for testing?
Stepan Subik
on 13 Mar 2020
As suspectected, the hours are 0-23 hours; the last two records of the file time field are
194905011800
194905011900
which are 18 and 19 hours in 24-hr time, not 12-hr time so must use 'HH' instead of 'hh'
>> datetime('194905011800','InputFormat','yyyyMMddhhmm')
Error using datetime (line 636)
Unable to convert '194905011800' to datetime using the format 'yyyyMMddhhmm'.
>>
>> datetime('194905011800','InputFormat','yyyyMMddHHmm')
ans =
datetime
01-May-1949 18:00:00
>>
See the doc for datetime format strings for all the details.
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Conversion in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!