How to read string data from a csv file?
58 次查看(过去 30 天)
显示 更早的评论
Hello.. I have a csv data file with 6columns. First column is string type and others are numeric. I don't know why I cant read data with 'csvread'. I saved csv file as xls then used xlsread. It can read my data but it's not reading 1st column may be because its a string. 1st column is like yyyy-mm-ddThh:mm:ss Is there any way to read 1st column and then split it into two columns, one for date and other for time? how to read this data using csvread as I don want to convert it to xls. Thanks
1 个评论
采纳的回答
更多回答(2 个)
Guillaume
2017-8-1
编辑:Guillaume
2017-8-1
Even simpler than xlsread is to use readtable which should be able to figure out the file format on its own.
data = readtable('yourfile.csv');
In particular, readtable should automatically detect that the first column is a datetime and decode it properly. If not it's trivial to convert it:
data(:, 1) = datetime(data(:, 1), 'InputFormat', 'yyyy-MM-ddTHH:mm:ss'); %or similar
Md Khaled Ben Islam
2018-4-15
1 个评论
CHIA HUNG MOU
2023-12-31
编辑:CHIA HUNG MOU
2023-12-31
Yes. readtable is good. For your reference:
% data: https://chris.userweb.mwn.de/book/pizza_delivery.csv
fn = fullfile('pizza_delivery.csv');
data = readtable(fn);
% time
y = cell2mat(table2cell(data(:, 3)));
% interception
x1 = ones(1, size(y,1));
% binary variable (so called dummy variable)
x2 = zeros(1, size(y,1));
x2_ = (table2cell(data(:, 4)));
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!