Reading .csv Files with different number of Columns
11 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Silvan Steiger
le 14 Mar 2018
Commenté : Silvan Steiger
le 18 Mar 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!
0 commentaires
Réponse acceptée
Akira Agata
le 16 Mar 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);
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Text Files dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!