how to read a table from a CSV file with multiple header

조회 수: 13 (최근 30일)
Carlos Alonso
Carlos Alonso 2020년 5월 26일
댓글: Carlos Alonso 2020년 5월 26일
Hello
I want to read a table from a CSV file that has 2 row header.
Time Speed Temp
s kph degC
1 12 24
2 13 25
3 14 26
How I can read the above table and display it showing the 2 row header.?
I have tried to do on different ways but only 1 row header is shown (see below)
3×3 table
Time Speed Temp
____ _____ ____
1 12 24
2 13 25
3 14 26
However I would like to see table as below
3×3 table
Time Speed Temp
s kph degC
1 12 24
2 13 25
3 14 26

답변 (1개)

Ruger28
Ruger28 2020년 5월 26일
편집: Ruger28 2020년 5월 26일
fname = 'MyCSV.csv';
headerlines = 2;
CSV_Data = dlmread(fname,',',headerlines,0); % filename, delimiter, row offset, column offset
% create table with pre-defined headers
TableHeaders = {'Time_s','Speed_kph','Temp_C'};
MyTable = table(CSV_Data(:,1),CSV_Data(:,1),CSV_Data(:,1),'VariableNames',TableHeaders);
  댓글 수: 3
Ruger28
Ruger28 2020년 5월 26일
Yes, sorry about that. I guess I read too fast.
Will you always know the headers, or are they dynamic?
Carlos Alonso
Carlos Alonso 2020년 5월 26일
Hi , the header will change dynamically, as I will be adding and removing columns. Thanks

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Statistics and Machine Learning Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by