counting vowels then removing them from a txt file

4 vues (au cours des 30 derniers jours)
Jake
Jake le 31 Mar 2023
Commenté : Jake le 1 Avr 2023
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 commentaire
Les Beckham
Les Beckham le 31 Mar 2023
You haven't actually read the file into Matlab. You just created a file pointer to it with fopen.
The fileread function is an easier way to read a text file into Matlab.
Also read the docs on regexp and regexprep. You can do this without a loop.
This doesn't do what you apparently think it does.
while x==1:length(sampletext.txt)
If you still want to do this with a loop, read about for and numel

Connectez-vous pour commenter.

Réponse acceptée

Image Analyst
Image Analyst le 31 Mar 2023
Sounds like homework. Here's a hint for how you can read in each line, one line at a time.
% Open the file for reading in text mode.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
lineCounter = 1;
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
% Read the next line.
textLine = fgetl(fileID);
lineCounter = lineCounter + 1;
end
% All done reading all lines, so close the file.
fclose(fileID);
You can also use readlines and ismember
  3 commentaires
Image Analyst
Image Analyst le 31 Mar 2023
Modifié(e) : Image Analyst le 31 Mar 2023
Looks like you didn't try any of my suggestions. Try this:
% Demo by Image Analyst to count the number of vowels in a text file.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format short g;
format compact;
baseFileName = 'input.txt';
fullFileName = fullfile(pwd, baseFileName)
% Open the file for reading in text mode.
fileID = fopen(fullFileName, 'rt');
% Read the first line of the file.
textLine = fgetl(fileID);
lineCounter = 1;
vowel_set = 'aAeEiIoOuU';
numVowels = 0; % Number of vowels encountered so far.
numCharacters = 0; % Number of total characters encountered so far (vowels + non-vowels).
while ischar(textLine)
% Print out what line we're operating on.
fprintf('%s\n', textLine);
[isAVowel, vowelSetIndex] = ismember(textLine, vowel_set);
% Get the number of vowels in this line
numVowels = numVowels + sum(isAVowel);
% Count the total number of characters
numCharacters = numCharacters + numel(textLine);
% Create a new text line without the vowels
vowellessTextLine = textLine(~isAVowel);
% Read the next line.
textLine = fgetl(fileID);
lineCounter = lineCounter + 1;
end
% All done reading all lines, so close the file.
fclose(fileID);
fprintf('Of the %d characters in "%s", %d (%.2f%%) were vowels.\n', ...
numCharacters, baseFileName, numVowels, 100*numVowels/numCharacters)
Adapt as needed.
Jake
Jake le 1 Avr 2023
thank you very much for your help!

Connectez-vous pour commenter.

Plus de réponses (1)

Aditya Srikar
Aditya Srikar le 31 Mar 2023
Modifié(e) : Aditya Srikar le 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.

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by