How to read text header data from a csv file along with numerical data

74 visualizaciones (últimos 30 días)
FB
FB el 14 de Abr. de 2021
Comentada: Walter Roberson el 14 de Abr. de 2021
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?

Respuestas (2)

Cris LaPierre
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")
T = 7×7 table
point time B:IRMCHG B:IRML01 B:IRMS01 B:IRML02 B:IRMS02 _____ _______ _________ _________ _________ _________ _________ 1 0 -10.212 0.014042 0.015095 0.015813 0.018463 2 8e-05 -0.024943 0.012861 0.013613 0.014188 0.016395 3 0.00016 -0.019955 0.011599 0.012088 0.012405 0.014114 4 0.00024 -0.019955 0.010515 0.010846 0.011015 0.012214 5 0.00032 -0.019955 0.009731 0.0099341 0.0099856 0.01079 6 0.0004 -0.024943 0.0091936 0.009289 0.009289 0.0098321 7 0.00048 -0.019955 0.0088214 0.0088671 0.0088214 0.0092412
hdrs = T.Properties.VariableNames
hdrs = 1×7 cell array
{'point'} {'time'} {'B:IRMCHG'} {'B:IRML01'} {'B:IRMS01'} {'B:IRML02'} {'B:IRMS02'}
plot(T.time,T.('B:IRMCHG'))
title(hdrs{3})

FB
FB el 14 de Abr. de 2021
Thanks a lot! Cris's answer did not resolve my problem fully. However, a variation of his approach solved the problem. The Matlab program below works for me for a multi-column csv file with first row header for each column and the rest being numerical data:
clc; clear all; close
fid='BLM\cbhat_blmdata_67678.csv'
fid='BLM\test.csv'
%Import the options of the csv file
opts=detectImportOptions(fid);
%Defines the row location of channel variable name
opts.VariableNamesLine = 1;
%Specifies that the data is comma seperated
opts.Delimiter =','; %Specifies that the data is comma seperated
%Read the table
T = readtable(fid,opts, 'ReadVariableNames', true);
% figure; plot(T.time,T.B_IRMCHG)
hdrs = T.Properties.VariableNames;
figure; plot(T.(hdrs{2}),T.(hdrs{3}))
title(hdrs{3})
  2 comentarios
Rik
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
Walter Roberson el 14 de Abr. de 2021
figure; plot(T.(hdrs{2}),T.(hdrs{3}))
could be
figure; plot(T{:,2},T{:,3})

Iniciar sesión para comentar.

Categorías

Más información sobre Low-Level File I/O en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by