Reading .csv Files with different number of Columns

10 visualizaciones (últimos 30 días)
Silvan Steiger
Silvan Steiger el 14 de Mzo. de 2018
Comentada: Silvan Steiger el 18 de Mzo. de 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!

Respuesta aceptada

Akira Agata
Akira Agata el 16 de Mzo. de 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);

Más respuestas (0)

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by