How to read string data from a csv file?

65 ビュー (過去 30 日間)
Kavita Navria
Kavita Navria 2017 年 8 月 1 日
編集済み: CHIA HUNG MOU 2023 年 12 月 31 日
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 件のコメント
Sai Ramya
Sai Ramya 2018 年 2 月 5 日
編集済み: Sai Ramya 2018 年 2 月 5 日
Don't mention any reading format like 'rb' ....it will read string type....use names= column names while creating a Excel sheet...

サインインしてコメントする。

採用された回答

KSSV
KSSV 2017 年 8 月 1 日
Use xlsread. Read about it.
  3 件のコメント
KSSV
KSSV 2017 年 8 月 1 日
[num,txt,raw] = xlsread('myfile')
This read entire data.....
Kavita Navria
Kavita Navria 2017 年 8 月 1 日
Thank you sir..This is reading my data. num= N x 6 (type double, skipped 1st column), raw= N x 7(type cell, all columns), txt= N x 7(type cell, 1st column is complete while for others its just showing column headings ) How may I split date and time from 'cell' type. I am using R2013a. I tried using strsplit but its not supporting cell2str, cell2table.

サインインしてコメントする。

その他の回答 (2 件)

Guillaume
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
  1 件のコメント
Guodong Cui
Guodong Cui 2018 年 8 月 17 日
readable is simple & useful! It works for me. Thank you!

サインインしてコメントする。


Md Khaled Ben Islam
Md Khaled Ben Islam 2018 年 4 月 15 日
For dealing with tabular CSV formatted data, readtable seems more flexible than xlsread.
  1 件のコメント
CHIA HUNG MOU
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 ExchangeData Import from MATLAB についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by