Process .csv file, combine first two rows into 1 (text rows) to make them the variables

1 view (last 30 days)
Hello, i have the next .csv file
I don't know how to process the data, if implement the function textscan or importdata. What i would like to do is to combine the first two rows to make them one row that contains the names of the variables, for example, A1 with A2 to get the first variable: TimeSweep_time_in_h.
Thank you very much.
Juan Pablo

Answers (2)

Jeremy Hughes
Jeremy Hughes on 17 Dec 2017
You could try using detectImportOptions (R2016b or later) and modify the VariableUnitsLine property to bring in the second line.
opts = detectImportOptions(filename)
opts.VariableNamesLine = 1;
opts.VariableUnitsLine = 2;
T = readtable(filename,opts);
Then you can use something like strcat to put them together
T.Properties.VariableNames = strcat( T.Properties.VariableNames,'_', T.Properties.VariableUnits);
(Untested, but I think this works)
Jeremy

Juan Pablo Martinez Fernandez
Thank you very much for your answer, i decided not to combine them and only work with the second row. I have another question, I have a table like the following:
What i would like to do is to get from this table another table only with the first column (timeinh) and the columns which names are Verlustegesamt_AnschlussiinMW_XX (Eliminate all the other columns that differ from this name).
How can i do it? I can´t find the way to do it with the function textscan.
Thank you vey much for your answers.
  1 Comment
Jeremy Hughes
Jeremy Hughes on 19 Dec 2017
It's possible with textscan, but you need to identify which columns correspond to which format specifiers and add a * to the ones you don't want. e.g. '%f%*s%f' would only import the two numeric fields and not the string in the middle.
I suggest following the same approach with import options, you can choose a subset to import with the SelectedVariableNames. The startsWith function will make matching these easy as well.
namesIDs = startsWith(opts.VariableNames,'Verlustegesamt_AnschlussiinMW');
opts.SelectedVariableNames = opts.VariableNames(namesIDs);
Jeremy

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!