How to read text header data from a csv file along with numerical data
55 次查看(过去 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!