How to read and scan a text file
4 次查看(过去 30 天)
显示 更早的评论
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.
采纳的回答
DGM
2021-8-31
编辑:DGM
2021-8-31
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 个评论
DGM
2021-9-6
编辑:DGM
2021-9-6
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);
更多回答(1 个)
Chunru
2021-8-31
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import and Export 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!