I am getting error Index exceeds matrix dimensions. What am I missing here?

1 view (last 30 days)
% Clear the screen
clc
% Open text file and read only
fid = fopen('file.txt', 'r');
% Define a set of vowels then use the strfind function
vowel_set = 'aAeEiIoOuU';
% Search for the vowels
vowels = 0;
non_vowels = [ ];
while ~feof(fid)
message=fgetl(fid);
% check for vowel line by line till the end of file
for pos = 1 : length(message)
c = message(pos);
if strfind(vowel_set, c)
vowels = vowels + 1;
else
non_vowels = [ non_vowels pos ];
end
end
end
fclose(fid);
% Display formatted results
m = sprintf('This file contains %d vowels.', vowels);
disp(m);
disp(['Message without vowels: ' fid(non_vowels)]);
  1 Comment
Adam
Adam on 20 Aug 2014
Please use the '{} Code' block option to format your question by selecting all that and pressing the button.

Sign in to comment.

Answers (2)

Geoff Hayes
Geoff Hayes on 20 Aug 2014
Adorn - the code in your question should be formatted so that it is readable. Please highlight it and press the {} Code button.
Running your code produces the following error
Index exceeds matrix dimensions.
Error in untitled4 (line 27)
disp(['Message without vowels: ' fid(non_vowels)]);
fid is a file descriptor, not an array. Your non_vowels are the positions in each line where there is a non-vowel, and so is an array. You cannot access fid to retrieve characters with certain indices (and bear in mind that the indices in non_vowels are on a per row basis, so with the exception of the first line, are not relative to their actual position in the message).
What are you trying to display here? The message without vowels?
If that is the case, then you should build the message without vowels at each line. Modify your code to include the following
% other initializations
% define cell array for message without vowels
msgWithoutVowels = {};
atLine = 1;
while ~feof(fid)
message=fgetl(fid);
% initialize line to empty string
msgWithoutVowels{atLine,1} = '';
% check for vowel line by line till the end of file
for pos = 1 : length(message)
c = message(pos);
if strfind(vowel_set, c)
vowels = vowels + 1;
else
% create the line without vowels
msgWithoutVowels{atLine} = [msgWithoutVowels{atLine} c];
end
end
% finished reading current line, so increment counter
atLine = atLine + 1;
end
and replace the final disp with
fprintf('Message without vowels: \n');
for k=1:length(msgWithoutVowels)
fprintf('%s\n',msgWithoutVowels{k});
end
There are only a handful of line changes in the above. Try it and see what happens!

Joseph Cheng
Joseph Cheng on 20 Aug 2014
Seeing how that was incredibly hard to read, i can only see it being the fid(non_vowels) at the very end. fid is the file identifier you used to open the text file (its a 1x1 variable such as 3, 4, 5 etc). so by going fid(non_vowels) you're trying to index all the non-vowel positions within a 1x1. what are you trying to accomplish with the non_vowel position?

Categories

Find more on Characters and Strings 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!