How to read and scan a text file

조회 수: 9 (최근 30일)
Ben Hischar
Ben Hischar 2021년 8월 31일
편집: DGM 2021년 9월 6일
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
KSSV
KSSV 2021년 8월 31일
Show us how your file is. Attach an example file. It can be done without loop as well.
Stephen23
Stephen23 2021년 8월 31일
편집: Stephen23 2021년 8월 31일
No loop required:
S = fileread('SampleText.txt');
N = nnz(ismember(lower(S),'aeiou'))

댓글을 달려면 로그인하십시오.

채택된 답변

DGM
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 = 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
Ben Hischar
Ben Hischar 2021년 9월 5일
DGM
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);
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);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Chunru
Chunru 2021년 8월 31일
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

카테고리

Help CenterFile Exchange에서 Text Files에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by