Error showing image after reading it

조회 수: 6 (최근 30일)
Will Pihir
Will Pihir 2021년 9월 4일
답변: Alamanda Ponappa Poovaya 2021년 9월 7일
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
Walter Roberson
Walter Roberson 2021년 9월 4일
That could happen if you had a variable named imshow
Will Pihir
Will Pihir 2021년 9월 4일
Thank you for that. I did have a variable named imshow because I hadn't cleared my workspace.

댓글을 달려면 로그인하십시오.

답변 (1개)

Alamanda Ponappa Poovaya
Alamanda Ponappa Poovaya 2021년 9월 7일
You can resolve this error by using the clear command before running your code

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by