How to read the first line (names variables) of a text file ?
조회 수: 44 (최근 30일)
이전 댓글 표시
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
채택된 답변
Geoff Hayes
2017년 7월 10일
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);
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Text Files에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!