CSV import with variable column headers
조회 수: 11 (최근 30일)
이전 댓글 표시
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
답변 (2개)
Felix
2016년 11월 20일
It should be noted that importdata is an extremely convenient alternative function for this and auto-detects the column headers.
댓글 수: 0
Geoff Hayes
2014년 6월 11일
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
Geoff Hayes
2014년 6월 11일
편집: Geoff Hayes
2014년 6월 11일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import and Analysis에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!