How to store string values from a for loop into a cell array?

조회 수: 10 (최근 30일)
Neil Bhattacharjee
Neil Bhattacharjee 2016년 8월 15일
편집: Moe_2015 2016년 8월 15일
Hi. I want the user to input an arbitrary number, and then set up a for loop that asks the question
"Enter a string" as many times as the user indicated above with the first input.
I then want to store all these string values into a cell array, and be able to retrieve these strings later.
How do I do this?
prompt = "Enter a number";
x = input(prompt)
for i=1:x
%Code that asks user to input string
end

채택된 답변

Moe_2015
Moe_2015 2016년 8월 15일
prompt = 'Enter a number: ';
x = input(prompt);
for i=1:x
user_string = input('Enter a string: ');
all_strings{i} = user_string;
end
  댓글 수: 3
Neil Bhattacharjee
Neil Bhattacharjee 2016년 8월 15일
Thanks for the help moe!
I know this might be a little too much, but is there any way I can make it so that I don't have to use quotations when i'm entering strings?
Thanks!
Moe_2015
Moe_2015 2016년 8월 15일
편집: Moe_2015 2016년 8월 15일
Yes, replace
user_string = input('Enter a string: ');
with
user_string = input('Enter a string: ','s');
This way you will not need any single quotations and you just have to enter whatever text you want as an input and it will treat it as a string.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by