Read first letter of each line of a .txt file

8 vues (au cours des 30 derniers jours)
Bob Whiley
Bob Whiley le 23 Fév 2015
Commenté : Image Analyst le 23 Fév 2015
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'

Réponses (1)

Image Analyst
Image Analyst le 23 Fév 2015
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 commentaires
Bob Whiley
Bob Whiley le 23 Fév 2015
Modifié(e) : Bob Whiley le 23 Fév 2015
It works, thanks!
Image Analyst
Image Analyst le 23 Fév 2015
Great. Can you mark the Answer officially as "Accepted" then? Thanks.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Simulink dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by