How do you store all return values from a while loop in a vector in matlab ?
이전 댓글 표시
I have this for now, but unfortunately it only stores the latest value of choice_breakfast.
while breakfast_choicerep == 'Y' || breakfast_choicerep == 'y'
choice_breakfast = menu('What do you eat for breakfast?',breakfast_items);
vector_choice_breakfast = [choice_breakfast]
breakfast_choicerep = input('Did you eat anything else for brekfast? (Y/N): ', 's');
end
답변 (1개)
jgg
2016년 4월 26일
0 개 추천
You can just store it as a vector:
vector_choice_breakfast = cell(1000,1); count = 1; while breakfast_choicerep == 'Y' | breakfast_choicerep == 'y'
choice_breakfast = menu('What do you eat for breakfast?',breakfast_items);
vector_choice_breakfast(count) = choice_breakfast;
count = count + 1;
breakfast_choicerep = input('Did you eat anything else for brekfast? (Y/N): ', 's');
endThe only issue is that you have to make sure you handle what happens when more than 1000 breakfast items are eaten, which could involve throwing an error and handling it (say by resizing the vector) or simply disallowing more output.
댓글 수: 4
Walter Roberson
2016년 4월 26일
You do not need to handle more than 1000 as an error: you could just let the array grow. Access will get slower and slower and eventually you will start swapping to disk after you get a few 10s of millions of items for the breakfast.
Harshita Jain
2016년 4월 27일
Harshita Jain
2016년 4월 27일
Try changing the brackets in the assignment instead:
vector_choice_breakfast{count} = choice_breakfast;
I thought you were saving character arrays, not floats.
카테고리
도움말 센터 및 File Exchange에서 App Building에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!