How to read text header data from a csv file along with numerical data
Show older comments
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?
Answers (2)
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})
FB
on 14 Apr 2021
2 Comments
Rik
on 14 Apr 2021
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
on 14 Apr 2021
figure; plot(T.(hdrs{2}),T.(hdrs{3}))
could be
figure; plot(T{:,2},T{:,3})
Categories
Find more on Cell Arrays 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!