read csv file with headers with readtable and get the header

23 vues (au cours des 30 derniers jours)
Pascal Guillot
Pascal Guillot le 15 Mar 2023
Commenté : Pascal Guillot le 17 Mar 2023
I have a data csv file which include an header with several lines. Each header line starts with the sign %
I have no problem to read the file with readtable and to get the data.
But I would like to get also the header lines which describe my data.
Any suggestion ?

Réponse acceptée

Walter Roberson
Walter Roberson le 15 Mar 2023
You will not be able to do this with a single call to readtable() .
Either process the lines separately or else use readcell(). Though with readcell() it is possible you would need to piece lines back together if there just happened to be commas in the lines.
If the files are not huge, sometimes it can be very effective to use fileread() to read the line as a character vector, then use regexp() to pull out the headers, then textscan() the remaining lines -- provided you know their format ahead of time. Though to be honest if you have date/time fields it can be a nuisance to get the %D %T formats right.
  2 commentaires
Pascal Guillot
Pascal Guillot le 17 Mar 2023
Thank you. I am going to test both.
Pascal Guillot
Pascal Guillot le 17 Mar 2023
so far, I extract the data with readtalble and the comments/header with readcell.
I use detectImportOptions with readcell to read only the lines without data.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by