2017-04-15 49 views
0

我有問題,我怎麼能從用戶多個輸入(每行一次)作爲一個字符串,並將其保存在數組內?關於在數組中保存字符串

我想是這樣的:

function[str] = get_data() 

st = ''; 

st{1} = input{'enter the first name','s'}; 
st{2} = input{'enter the first name','s'}; 

str = strings(st) 

end 

回答

0

事先聲明你的小區。不是一個字符串。然後你可以填入輸入。

function[str] = get_data() 

st = cell(1,2); 
st{1} = input('enter the first name','s'); 
st{2} = input('enter the last name','s'); 
str=[st{:}] %if you want to convert it back to a string 
%str = strcat(st{1},'_',st{2}) %if you want a _ between the inputs 

end 
0

試試下面的代碼:

st = []; 
st{1} = input('enter the first name: ','s'); 
st{2} = input('enter the last name: ','s'); 
str = strcat(st{1},'_',st{2})