counting vowels then removing them from a txt file
    조회 수: 5 (최근 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
      
 2023년 3월 31일
				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)
채택된 답변
  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);
댓글 수: 3
  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
    
 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.
댓글 수: 0
참고 항목
카테고리
				Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



