How to read the third line of a .txt file starting in the middle of the line?
조회 수: 5 (최근 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!