필터 지우기
필터 지우기

Read first letter of each line of a .txt file

조회 수: 16 (최근 30일)
Bob Whiley
Bob Whiley 2015년 2월 23일
댓글: Image Analyst 2015년 2월 23일
Given an input .txt file, how can I extract the first letter of each line of that file.
Ex: file.txt is
I’ve worn a black suit
my entire life. It suits the war
my eyes ignite.
My sins sit on my lap,
bald, blind, desperate
for the mercy of lost roads,
glottal white lines.
Would output: 'Immmbfg'

답변 (1개)

Image Analyst
Image Analyst 2015년 2월 23일
Did you see the help for fgetl()? Just modify it to add an array for the first characters of the lines:
fid = fopen('fgetl.m');
tline = fgetl(fid);
lineNumber = 1;
while ischar(tline)
disp(tline)
firstCharacters(lineNumber) = tline(1);
tline = fgetl(fid);
lineNumber = lineNumber + 1;
end
fclose(fid);
  댓글 수: 2
Bob Whiley
Bob Whiley 2015년 2월 23일
편집: Bob Whiley 2015년 2월 23일
It works, thanks!
Image Analyst
Image Analyst 2015년 2월 23일
Great. Can you mark the Answer officially as "Accepted" then? Thanks.

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

카테고리

Help CenterFile Exchange에서 String Parsing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by