필터 지우기
필터 지우기

Reading strings from text file to a MATLAB cell array

조회 수: 9 (최근 30일)
Adam Hicks
Adam Hicks 2017년 11월 27일
댓글: Lennah Etyang 2020년 6월 1일
I have a text file with data that looks like this:
Ta Example Name1
Tn Example Name2
Th Example Name3
Td Example Name4
Tg Example Name5
The 'Ta', 'Tn', etc. will always be the same length of 2 characters but 'Example Name1' and 'Example Name2' will not. I'd like to read this information into a cell array or other array in MATLAB so that the first column contains the identifier (Ta, Tn, Th, etc.) and the second column contains the name.
I've tried several approaches using a CSV file but it was difficult to append the file with a cell array of strings (not supported with xlswrite, dlmwrite, etc.).

채택된 답변

KL
KL 2017년 11월 27일
Try textscan,
fid = fopen('file.txt');
data = textscan(fid,'%s%s%s');
fclose(fid);
  댓글 수: 2
Adam Hicks
Adam Hicks 2017년 11월 27일
This worked great, thanks! Now I can just concatenate elements where needed.
Lennah Etyang
Lennah Etyang 2020년 6월 1일
I have been stuck at this point for a while and now it has just worked. Thanks for the solution.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by