Steve
Steve

Reputation: 644

Keeping whitespace in csv headers (Matlab)

So I'm reading in a .csv file, and it all works as I want bar one thing. The headers of the data have spaces, which I want later for displaying data to the user. However, these spaces get stripped when the csv file is read in via readtable (as they get used as the variable names). Again, no problem with this per se, but I still need the unmodified strings as well.

Two additional notes:

  1. I'm happy for the strings to be stored separately from the main table if that makes things easier.
  2. The actual .csv file I'm reading in is reasonably large (about 2 million data points) so from a computational cost side of things, the less reading of the file the better

Example read in code:

File = 'example.csv';
Import_Options = detectImportOptions( File, 'NumHeaderLines', 0 );    
Data = readtable( File )

Example csv file (example.csv):

"this","is","an","example test"
"1","1","2","3"
"3","1","4","1"
"hot","hot","cold","hot"

Upvotes: 1

Views: 226

Answers (1)

Adriaan
Adriaan

Reputation: 18207

You can simply read the first line with fgetl, thus grabbing the headers, before reading the entire file with readtable.

Upvotes: 2

Related Questions