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_history
end;
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!

 Réponse acceptée

Stephen23
Stephen23 le 14 Nov 2015
Modifié(e) : Stephen23 le 14 Nov 2015
There is nothing wrong. That is how MATLAB displays cell contents if the cell contents are not a simple scalar array or a short string, in which case it prints the size and class of the variable in the cell:
>> C = {1,[3,4]}
C =
[1] [1x2 double]
The content of the cell are still there:
>> C{2}
ans =
3 4
Instead of relying on printing data to your command window, you should try using the Variable Editor: simply double click on a variable in your workspace panel to open it in the Variable Editor. The Variable Editor lets you click through all levels of cell arrays and structures too, so you can view all of the data.

2 commentaires

Lisa Ka
Lisa Ka le 14 Nov 2015
Oh okay! Thank you! I am just used to the content of the list being shown. I tried to access the elements of the cell array with all_guesses{1,1} for the first guess in the first round but it shows me the whole cell (all the guesses from that round). How do I do it? Thanks!
Lisa Ka
Lisa Ka le 14 Nov 2015
thank you!

Connectez-vous pour commenter.

Plus de réponses (1)

If you do want to display the actual contents of the cell, rather than just their size and class, you can do this with celldisp. See https://www.mathworks.com/help/matlab/ref/celldisp.html.
For instance, in Stephen Cobeldick's example we have
C = {1,[3,4]};
>> disp(C);
[1] [1×2 double]
>> celldisp(C)
C{1} =
1
C{2} =
3 4

Catégories

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by