How to read the first line (names variables) of a text file ?
172 views (last 30 days)
Show older comments
Jeremy Schmid
on 10 Jul 2017
Commented: Jeremy Schmid
on 10 Jul 2017
Hello,
I didn't find how to read with textscan the first line of my text file(.csv).
I want to read and create a cell with all the names of variables.
Myfile.csv :
Times;Q1;Q2;Q3;Q4;Q5;Q6
01.03.2016 00:00;25.6;7.4;5;235;8;0
01.03.2016 00:15;25.5;7.4:9;456;7;0
01.03.2016 00:30;25.5;7.4;8;234;89;45
Script:
Names = textscan(fid,' %s %s %s %s %s %s %s ','Delimiter',';');
the result is that Names is a 1x7cell but with nothing inside the cell.
I want that Names have all the name's variable inside the cell.
Example:
Names [Times Q1 Q2 Q3 Q4 Q5 Q6 ]
Actually I want the opposite of the headerline option.
0 Comments
Accepted Answer
Geoff Hayes
on 10 Jul 2017
Jeremy - you can try specifying the number of lines to read (using your format specifier) as
Names = textscan(fid,' %s %s %s %s %s %s %s ', 1, 'Delimiter',';');
Using R2014a, this will return a 1x7 cell array where each element is a name of one of your variables.
Alternatively, you could just read the first line of your file as
fid = fopen('myTextFile.csv');
varNames = strsplit(fgetl(fid), ';');
fclose(fid);
More Answers (0)
See Also
Categories
Find more on Text Files 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!