counting vowels then removing them from a txt file

7 次查看(过去 30 天)
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 个评论
Les Beckham
Les Beckham 2023-3-31
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

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2023-3-31
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 个评论
Image Analyst
Image Analyst 2023-3-31
编辑:Image Analyst 2023-3-31
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.

请先登录,再进行评论。

更多回答(1 个)

Aditya Srikar
Aditya Srikar 2023-3-31
编辑:Aditya Srikar 2023-3-31
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.

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by