필터 지우기
필터 지우기

store the input into the cell array with a loop

조회 수: 2 (최근 30일)
jarvan
jarvan 2014년 11월 11일
댓글: jarvan 2014년 11월 13일
I try to store three input into a cell array by using loop there are some parts I messed up. It said undefined 'game', what should I change with my code?
game1= input('Enter your game :','s')
game2= input('Enter your game :','s')
game3= input('Enter your game :','s')
n = 3;
for i = 1:n
i = cell{game*n}
thegame(i);
end
disp(thegame)

채택된 답변

Guillaume
Guillaume 2014년 11월 11일
Your loop makes no sense at all.
The simplest thing would be to store the return value of input straight into the cell array:
game{1}= input('Enter your game :','s')
game{2}= input('Enter your game :','s')
game{3}= input('Enter your game :','s')
Otherwise, you have to use build the name of the variable as a string (using sprintf for example) and then eval it to get the content of the variable:
for gamenumber = 1:3
thegame{gamenumber} = eval(sprintf('game%d', gamenumber));
end
  댓글 수: 3
Guillaume
Guillaume 2014년 11월 12일
You use either my first three lines of code on their own, or your first three lines of code plus my for loop.
jarvan
jarvan 2014년 11월 13일
it works now , thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Strategy & Logic에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by