CSV import with variable column headers
17 views (last 30 days)
Show older comments
Hey there, I'm new to MATLAB,and I've searched the forums and MATLAB help and can't seem to find an straightforward way to create a generic CSV import script. I'm looking to take a generated CSV file after a test run where the number of parameters can vary from test to test and import it to MATLAB for post processing.
The format of the CSV file is consistent with a format as such:
Test Name
Test Date
Start time
Parameter ID1, PID2, PID3, ... PIDn
EU1, EU2, EU3, ... EUn
Data 1, data 2, data 3, ... Data n
I know that I want to take row say 6 and make that my variable names, and row 8 is the start of the data, but column width (e.g. channels) could change depending on test conditions.
Cheers
0 Comments
Answers (2)
Felix
on 20 Nov 2016
It should be noted that importdata is an extremely convenient alternative function for this and auto-detects the column headers.
0 Comments
Geoff Hayes
on 11 Jun 2014
Rather than creating n variables for each column of data, why not just create a mxn matrix where n is the number of columns in your file. Reading the CSV data becomes easy
data = csvread('test.txt',8);
where 8 is the row that you want to start reading the data from. The first seven (header) rows are ignored and you can now do any post processing on data. (This assumes the format of the file is strictly the header of seven lines followed by m rows of numeric data.)
2 Comments
Geoff Hayes
on 11 Jun 2014
Edited: Geoff Hayes
on 11 Jun 2014
Note that in the above command, csvread, the row argument is zero-based. So if you want the 8th row, you must supply 7 instead of the 8 that I mention.
If you just want the Parmeter IDs that are in say row 6, then you could do the following
fid = fopen('test.txt');
paramIds = textscan(fid,'%s',1,'HeaderLines',5);
fclose(fid);
paramIds will be a single-element cell array with the contents of the sixth line from your file (so comma-separated line of parameter ids). In the inputs to textscan, we indicate that we just want one row with 1, and that there are 5 header rows to skip.
For more info on these commands, type doc textscan and doc csvread.
See Also
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!