필터 지우기
필터 지우기

Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

question on reading txt file

조회 수: 3 (최근 30일)
John
John 2013년 10월 17일
마감: MATLAB Answer Bot 2021년 8월 20일
have a following txt file
DATA MATRIX
1 2 3
1 4 5
1 5 7
END
DATA TERMINALS
TP 13 26
TP 128 32
TP 164 45
TP 243 74
TP 499 97
END
EOF
I want to create a matrix M using this data. and a separate array T for the terminals.Please help.
  댓글 수: 1
Cedric
Cedric 2013년 10월 18일
When you say "matrix" for terminals, is it a numeric array with only numbers, or a cell array including 'TP' or whatever code there is at the beginning of each line?

답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 10월 17일
편집: Azzi Abdelmalek 2013년 10월 17일
fid = fopen('file.txt');
line1 = fgetl(fid);
res={line1};
while ischar(line1)
line1 = fgetl(fid);
res{end+1} =line1
end
fclose(fid);
res(end)=[]
idx1=find(cellfun(@(x) isequal(x,'DATA MATRIX'),res));
idx2=find(cellfun(@(x) isequal(x,'DATA TERMINALS'),res));
A=res(idx1+1:idx2-2)
B=res(idx2+1:end-1)

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

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

Start Hunting!

Translated by