Reading .csv Files with different number of Columns

조회 수: 11 (최근 30일)
Silvan Steiger
Silvan Steiger 2018년 3월 14일
댓글: Silvan Steiger 2018년 3월 18일
Hi There
I'm trying to import data from various .csv Files with different numbers of columns. I want to write a function that can import data from .csv-Files without knowing the number of columns beforehand. All columns must be read and imported.
Example of data.csv:
"datetime";"SensorId_3";"SensorId_4";"SensorId_13"
"2017-06-01 00:00:05";"9999";"4444";"5555"
"2017-06-01 00:00:15";"9999";"4444";"5555"
"2017-06-01 00:00:25";"9999";"4444";"5555"
"2017-06-01 00:00:35";"9999";"4444";"5555"
The .csv are always written in this style but the Number of Columns can change. So far I've tried the Matlab Import Data Function to write a function for Importing data but it doesn't work for other Files with different number of columns. Also I've done some experimenting with "textscan" and "readtable", but didn't get to a solution.
With "readtable" i cannot omit the quotation marks when reading (yes, I've tried formatSpec).
With textscan, i can get my data into one Vector (More or less I've copied the generated code from the Matlab Import Function):
datavector =
2017-06-01 00:00:05
9999
4444
5555
2017-06-01 00:00:15
9999
4444
...
Thanks for any advice!

채택된 답변

Akira Agata
Akira Agata 2018년 3월 16일
In R2017b and R2018a, readtable runction could read your data. If the number of columns changes, how about changing data-type after reading CSV data, like:
T = readtable('data.csv');
T.datetime = datetime(T.datetime);
  댓글 수: 1
Silvan Steiger
Silvan Steiger 2018년 3월 18일
I've tried this and it works perfectly. Thanks very much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by