How to read text header data from a csv file along with numerical data
조회 수: 56 (최근 30일)
이전 댓글 표시
My test.csv file is as follows:test.csv. I would like to read header for each column and numerical data as well.
My MATLAB program is
fid='BLM\test.csv'; delimiterIn = ',';% ',' seperated data
headerlinesIn = 1;% Ignore 1st header line
A = importdata(fid,delimiterIn,headerlinesIn);
t=A.data(:,2);a=A.data(:,4);
figure; plot(t,a)
How to read the header test and use it later as title to my plot?
댓글 수: 0
답변 (2개)
Cris LaPierre
2021년 4월 14일
Use readtable with VariableNamingRule set to true. Then extract the data you need from the table.
T = readtable('test.csv',"VariableNamingRule","preserve")
hdrs = T.Properties.VariableNames
plot(T.time,T.('B:IRMCHG'))
title(hdrs{3})
댓글 수: 0
FB
2021년 4월 14일
댓글 수: 2
Rik
2021년 4월 14일
A few remarks:
- clear all is equivalent to restarting Matlab. You probably don't need that. Using clear or clearvars instead is generally enough
- if you don't dump everything to the command window by skipping the ; symbols, you can make deliberate choices about which variables to print. That would also remove the need for clc.
- your use of fid as a variable name is confusing. Generally people use that for the file identifier, which is a handle to a file and is generated with fopen.
- you also didn't format your code as code. https://www.mathworks.com/matlabcentral/answers/help/rtc#rtc_summary
Walter Roberson
2021년 4월 14일
figure; plot(T.(hdrs{2}),T.(hdrs{3}))
could be
figure; plot(T{:,2},T{:,3})
참고 항목
카테고리
Help Center 및 File Exchange에서 Low-Level File I/O에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!