Problem with storing data into cell aray
古いコメントを表示
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!
採用された回答
その他の回答 (1 件)
Caitlin Lienkaemper
2019 年 1 月 8 日
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.
C = {1,[3,4]};
>> disp(C);
[1] [1×2 double]
>> celldisp(C)
C{1} =
1
C{2} =
3 4
カテゴリ
ヘルプ センター および File Exchange で Structures についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!