Reading csv files starting from a different row

I have this csv file which I would like to import into Matlab. The "readtable" function allows me to import the csv into matlab easily into a table format. It automatically makes the first row the column names and assigns the cell A1 "Var1" since it is empty.
However, what I would like it to do is to read assign the column names according to the 3rd row since that contains the proper headers.
Is it always possible to locate the row containing " time(s) " and make that particular the header when using the readtable function?
Or is there an alternative method available that is accomplish the tasks mentioned above?
edit: attached the csv file

2 comentarios

KALYAN ACHARJYA
KALYAN ACHARJYA el 24 de Feb. de 2021
Editada: KALYAN ACHARJYA el 24 de Feb. de 2021
Please attach the data file
See detectImportOptions
Hariharan MK
Hariharan MK el 24 de Feb. de 2021
Hi I have attached the data file like you have requested

Iniciar sesión para comentar.

 Respuesta aceptada

Mohammad Sami
Mohammad Sami el 24 de Feb. de 2021
Editada: Mohammad Sami el 24 de Feb. de 2021
You can use the import options to change the behaviour of the readtable function.
a = delimitedTextImportOptions('VariableNamesLine',3,'DataLines',5);
% or
% a = detectImportOptions('myfile.csv','VariableNamesLine',3,'Range',5);
b = readtable('myfile.csv',a);
% use the following if excel file
% a = detectImportOptions('testfile.xlsx','VariableNamesRange','A3','DataRange','A5');

6 comentarios

Hariharan MK
Hariharan MK el 24 de Feb. de 2021
what if i don't know which row the VariableNamesLine will be?
The only thing I know is that the headers will always be same regardless of whichever excel file I pick.
Mohammad Sami
Mohammad Sami el 24 de Feb. de 2021
Can you provide two example files. One way you can try is to use detect import options function without specifying the options. Then use the detected values to calculate where the header and data are. Then you can call detect import options again using the values you calculated.
Hariharan MK
Hariharan MK el 25 de Feb. de 2021
Hi I have attached 2 example files here.
So do you mean to use ...
opts = detectImportOptions('matlabqn_file1.csv')
I understand that using this will provide me with where the DataLines are. For the case of matlabqn_file1.csv, it will output [ 5 Inf ]. And knowing this I will then be able to identify the headers are found in the 3rd row.
I am not sure how to call the detect import options again using the values I calculated?
Hi
I managed to figure it out, since I always know that the headers appear 2 rows before the DataLines, I could change the
opts.VariableNamesLine = opts.DataLines(1)-2;
data = readtable("matlabqn_file1.csv",opts);
This method then allows me to select a particular row to become my header.
Thank you for your help!
Mohammad Sami
Mohammad Sami el 25 de Feb. de 2021
Great. :)

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 24 de Feb. de 2021

Comentada:

el 25 de Feb. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by