counting vowels then removing them from a txt file
Show older comments
fid = fopen('sampletext.txt' , 'r');
vowel_set = 'aAeEiIoOuU';
vowels = 0;
non_vowels = [ ];
while x==1:length(sampletext.txt)
c = sampletext.txt;
if strfind(vowel_set, c)
vowels = vowels + 1;
else
non_vowels = [ non_vowels pos ];
end
end
m = sprintf('Found %d vowels.', vowels);
disp(m);
disp(['Message without vowels: ' message(non_vowels)]);
very new to code, not quite sure what im doing wrong. any help would be appreciated :)
1 Comment
Les Beckham
on 31 Mar 2023
You haven't actually read the file into Matlab. You just created a file pointer to it with fopen.
This doesn't do what you apparently think it does.
while x==1:length(sampletext.txt)
Accepted Answer
More Answers (1)
Aditya Srikar
on 31 Mar 2023
Edited: Aditya Srikar
on 31 Mar 2023
Hi Jake,
These are the few mistakes in your code :
1) fopen() is used to open a file. To read fata from the file, you have to use fscanf().
Below is the syntax to open and read data from a file.
fileID = fopen('sampletext.txt','r');
formatSpec = '%c';
data = fscanf(fileID,formatSpec);
Link to documentation
2) The syntax of while loop is wrong.
Link to documentation - while loop
You can also use for loop to iterate over the string
for loop syntax to iterate over a string
data = 'abcd'
for c = data
disp(c)
end
Link to documentation - for loop
3) The syntax to access character of string at given position is
s = 'Welcome to MATLAB'
disp(['Character at 4th position is' s(4)])
4) You have not defined the function message() in your code.
But you have invoked message() in the last line.
Hope it helps.
Categories
Find more on Simulink Environment Customization 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!