How to read text header data from a csv file along with numerical data
74 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (2)
Cris LaPierre
el 14 de Abr. de 2021
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 comentarios
FB
el 14 de Abr. de 2021
2 comentarios
Rik
el 14 de Abr. de 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
el 14 de Abr. de 2021
figure; plot(T.(hdrs{2}),T.(hdrs{3}))
could be
figure; plot(T{:,2},T{:,3})
Ver también
Categorías
Más información sobre Low-Level File I/O en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!