Hello,
I am very new to matlab and I have run into a problem with cell arrays. My homework was to create a classic guess-a-number-game with a limited amount of rounds and trials within the round. I want to store the guesses for each round in a vector and append it to the cell array at the end of the round. However, my cell array doesn't show me the actual elements (like vectors with guesses) but looks like this: all_guesses =
[1x5 double] [1x5 double] [10]
Here's my code:
rounds = 3;maxtrials = 5;guess_history = [] %to create a vector
all_guesses = {} % cell array with the history of guesses for all three rounds
for r = 1:rounds %loop for three rounds
fprintf('\n Welcome to "Guess the Number"'); fprintf('\n This is round number: (%i). ',r); secret = randi(10) trial = 0; while true trial = trial +1; if trial > maxtrials %check
fprintf('Maximum number of trials reached!\n \n') break; guess = input('have a guess: '); guess_history(trial) = guess if guess == secret %the actual game
fprintf ('you are right, it is %i\n', guess) break %it breaks the while loop
elseif guess < secret fprintf ('%i is too low\n ', guess) else fprintf ('%i is too high\n', guess) end; end; all_guesses{r} = guess_history clear guess_historyend;save /path/guesses.m all_guesses
I also tried to store it like this: all_guesses{r, trial} = guess after the guess is being done, but it just creates a weird matrix.
Do you have any idea of what might be wrong here? Thanks!
Best Answer