How to read and scan a text file
4 views (last 30 days)
Show older comments
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.
Accepted Answer
DGM
on 31 Aug 2021
Edited: DGM
on 31 Aug 2021
Attached is an example file:
inputfile = fileread('SampleText.txt'); % make sure file exists?
Number_of_vowels = vowelcounts(inputfile) % original
Number_of_vowels = vowelcounts2(inputfile) % simplified
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 Comments
DGM
on 6 Sep 2021
Edited: DGM
on 6 Sep 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);
fprintf('The following %d lines contain vowels:\n',nvl)
for l = 1:nvl
fprintf('\t%s\n',vowellines{l})
end
fprintf('The following %d lines contain no vowels:\n',nvl)
for l = 1:nnvl
fprintf('\t%s\n',novowellines{l})
end
% 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);
More Answers (1)
Chunru
on 31 Aug 2021
s = fileread('SampleText.txt');
Number_of_vowels = vowelcounts(s);
fprintf('Number of vowels: %d\n', Number_of_vowels);
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
0 Comments
See Also
Categories
Find more on Data Import and Export in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!