Error showing image after reading it
1 回表示 (過去 30 日間)
古いコメントを表示
Hi guys,
I am having trouble displaying an image at the end of my code and keep getting provided with the error:
"Array indices must be positive integers or logical values"
Any suggestions?
The code is at the bottom but I included the whole script just in case you needed it to answer the question. I have also had success displaying images before.
Thanks
%Hangman
%Calling readDictionary function and assigning it to variable to be read
wordList = readDictionary("OxfordDictionary.txt");
%Calling chosenWord function so that word can be indexed and converted to
%string
hiddenWord = chosenWord(wordList);
%Calling hyphenWord function to create vector of hypens = to length of word
whatUserSees = hyphenWord(hiddenWord);
%Clearing command window
clc;
%Welcome message
fprintf("You are now playing hangman and must guess a word from the Oxford Dictionary\n");
fprintf("\n");
%Alerting the user of the length of the word and providing a visual
%representation
fprintf("Your random word has %d letters\n", length(whatUserSees));
fprintf("%s\n", whatUserSees);
%Initialising variables to be used in while and for loop
lives = 6;
correctlyGuessedLetters = 0;
%While loop running conditions are that lives are not 0 and # of correctly
%guessed letters =/= the length of the hidden word
while ~(lives == 0 || correctlyGuessedLetters == length(hiddenWord))
%Asking the user for a string input
guessedLetter = input("Enter a letter: ", "s");
%Account for uppercase inputs by converting to lowercase
lowercaseGuess = lower(guessedLetter);
%Scanning the hidden word to see if the user input matches any of the
%elements
scanningWord = strfind(hiddenWord, lowercaseGuess);
%Determining if the user has correctly guessed more than one letter in
%one guess
NuCorrectlyGuessedLetters = length(scanningWord);
%Incrementing # of correctly guessed letters by the # of letters the
%user guessed correctly in previous turn
correctlyGuessedLetters = correctlyGuessedLetters + NuCorrectlyGuessedLetters;
%Creating condition to see if hyphens need to be replaced with guessed
%letter
if NuCorrectlyGuessedLetters >= 1
%Reading length of letters guessed
for i = 1:length(scanningWord)
%Converting each element which has been correctly guessed from
%hypen to appropriate letter
whatUserSees(scanningWord(i)) = lowercaseGuess;
end
%Displaying to the user that they have correctly guessed a letter
%and updating what they see
fprintf("You have correctly guessed a letter\n")
fprintf("%s\n", whatUserSees)
%Using else statement to deduct life if above conditions are not
%met
else
lives = lives - 1;
%Displaying to user that they have incorrectly guessed a
%letter and how many lives they have left
fprintf("Your guess was incorrect\n")
fprintf("You have %d lives remaining\n", lives)
end
end
%Clearing command window
clc;
%End statement to determine whether user has won or lost
if (lives == 0)
fprintf("You have lost :(\n");
fprintf("Your word was: %s\n", hiddenWord);
loser = imread('youlose.png');
imshow(loser);
else
fprintf("You have won :)\n");
fprintf("Your word was: %s\n", hiddenWord);
end
2 件のコメント
回答 (1 件)
Drishan Poovaya
2021 年 9 月 7 日
You can resolve this error by using the clear command before running your code
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!