How to read and scan a text file

4 visualizaciones (últimos 30 días)
Ben Hischar
Ben Hischar el 31 de Ag. de 2021
Editada: DGM el 6 de Sept. de 2021
Hi all
Currently counting the number of vowels with a program but can not get my text file to open and be read by the script. I feel im missing something simple or my program is just completely wrong.
this is what i have.
inputfile = fileread('SampleText.txt');
Number_of_vowels = vowelcounts(s);
function w = vowelcounts(s)
w=0;
l=length(s);
for i=1:l
if s(i)=='a' || s(i)=='e' || s(i)=='i' || s(i)=='o' || s(i)=='u'
w=w+1;
else
continue
end
end
end
and this is the error
>> Vowel_count(SampleText.txt)
Unable to resolve the name SampleText.txt.
the program nees to display at the end "This file contains ... vowels."
any help is greatly apprecieted thanks.
  2 comentarios
KSSV
KSSV el 31 de Ag. de 2021
Show us how your file is. Attach an example file. It can be done without loop as well.
Stephen23
Stephen23 el 31 de Ag. de 2021
Editada: Stephen23 el 31 de Ag. de 2021
No loop required:
S = fileread('SampleText.txt');
N = nnz(ismember(lower(S),'aeiou'))

Iniciar sesión para comentar.

Respuesta aceptada

DGM
DGM el 31 de Ag. de 2021
Editada: DGM el 31 de Ag. de 2021
Attached is an example file:
inputfile = fileread('SampleText.txt'); % make sure file exists?
Number_of_vowels = vowelcounts(inputfile) % original
Number_of_vowels = 96
Number_of_vowels = vowelcounts2(inputfile) % simplified
Number_of_vowels = 96
function w = vowelcounts(s)
w=0;
l=length(s);
for i=1:l
if s(i)=='a' || s(i)=='e' || s(i)=='i' || s(i)=='o' || s(i)=='u'
w=w+1;
else
continue
end
end
end
function w = vowelcounts2(s)
w = sum(ismember(lower(s),['a' 'e' 'i' 'o' 'u']));
end
  3 comentarios
Ben Hischar
Ben Hischar el 5 de Sept. de 2021
DGM
DGM el 6 de Sept. de 2021
Editada: DGM el 6 de Sept. de 2021
Sorry. I missed the prior notification. Things get buried too easily in this new system.
I'm not quite sure what exactly you're asking for, but let's start with this (I've changed the sample file):
inputfile = fileread('SampleText.txt'); % make sure file exists?
inputfile = split(inputfile,char(10)); % split on newline (version-agnostic)
% vowel count per line
vcl = cell2mat(cellfun(@vowelcounts,inputfile,'uniform',false));
vctotal = sum(vcl);
vowellines = inputfile(vcl>0);
nvl = numel(vowellines);
novowellines = inputfile(vcl==0);
nnvl = numel(novowellines);
% do output
fprintf('This file contains %d vowels in total\n',vctotal);
This file contains 101 vowels in total
fprintf('The following %d lines contain vowels:\n',nvl)
The following 5 lines contain vowels:
for l = 1:nvl
fprintf('\t%s\n',vowellines{l})
end
The conscious and intelligent manipulation of the organized habits and opinions of the masses is an important element in democratic society. Those who manipulate this unseen mechanism of society constitute an invisible government which is the true ruling power of our country. -- Bernays, 1928 kelalksjdlk4iaa
fprintf('The following %d lines contain no vowels:\n',nvl)
The following 5 lines contain no vowels:
for l = 1:nnvl
fprintf('\t%s\n',novowellines{l})
end
zzxr44zkzkzkzlzlzkkzklzkjzlkzjlkjzxczxcjzlkjxclkzjxclkzjxclk zlxjclzkjxc zxlcjzlxkcj klljzlkcx
% this is the same function renamed
function w = vowelcounts(s)
w = sum(ismember(lower(s),['a' 'e' 'i' 'o' 'u']));
end
That lists lines containing vowels and lines containing no vowels. I did that simply for sake of clarity. You can use just the part you need. That said, if you want each instance of a vowel highlighted on each line, that gets quite a bit more complicated.
If you want to dump the output to a file instead of to console, you can do that too:
% dump to a file instead
fid = fopen('outputfile.txt','w');
fprintf(fid,'This file contains %d vowels in total\n',vctotal);
fprintf(fid,'The following %d lines contain vowels:\n',nvl);
for l = 1:nvl
fprintf(fid,'\t%s\n',vowellines{l});
end
fclose(fid);

Iniciar sesión para comentar.

Más respuestas (1)

Chunru
Chunru el 31 de Ag. de 2021
s = fileread('SampleText.txt');
Number_of_vowels = vowelcounts(s);
fprintf('Number of vowels: %d\n', Number_of_vowels);
Number of vowels: 12
function w = vowelcounts(s)
w=0;
l=length(s);
for i=1:l
if s(i)=='a' || s(i)=='e' || s(i)=='i' || s(i)=='o' || s(i)=='u'
w=w+1;
else
continue
end
end
end

Categorías

Más información sobre Data Import and Export en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by