How to read and scan a text file
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.
2 Comments
KSSV
on 31 Aug 2021
Show us how your file is. Attach an example file. It can be done without loop as well.
No loop required:
S = fileread('SampleText.txt');
N = nnz(ismember(lower(S),'aeiou'))
Accepted Answer
More Answers (1)
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
Categories
Find more on Language Support 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!