How to read the third line of a .txt file starting in the middle of the line?
2 ビュー (過去 30 日間)
古いコメントを表示
I have a text file containing student information in the following format:
School: xxxx
Grade: xxx
Student Name: LAST, FIRST
I want to read the third line and save the student last and first names as separate structure arrays formatted as Student.Name.Last and Student.Name.First
I am struggling to read exclusively the third line but only start as where the last name begins and then stop reading at the comma between the first and last name. My code currently returns the entire third line in the txt file.
fid = fopen('MyFile.txt');
linenum = 3;
C = textscan(fid,'s',1,'delimiter','\n', 'headerlines',linenum-1);
fclose(fid);
0 件のコメント
採用された回答
Walter Roberson
2022 年 11 月 4 日
編集済み: Walter Roberson
2022 年 11 月 4 日
You cannot start reading in the middle of a line (except in the case that you happen to know exactly how many bytes it is from the beginning of the file, or how many bytes from whereever you are currently positioned in the file.)
Try
C = textscan(fid,'Student Name: %s,%s', 1, 'delimiter', ',', 'headerlines',linenum-1);
Possibly you will instead need
C = textscan(fid,'Student Name: %s%s', 1, 'delimiter', ',', 'headerlines',linenum-1);
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Characters and Strings についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!