Reading .csv Files with different number of Columns

10 Ansichten (letzte 30 Tage)
Silvan Steiger
Silvan Steiger am 14 Mär. 2018
Kommentiert: Silvan Steiger am 18 Mär. 2018
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!

Akzeptierte Antwort

Akira Agata
Akira Agata am 16 Mär. 2018
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);

Weitere Antworten (0)

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by