关于在数组中保存字符串

时间:2017-04-15 03:14:54

标签: matlab

我有疑问如何从用户获得多个输入(每行一次)作为字符串并将其保存在数组中?

我试过这样的事情:

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

2 个答案:

答案 0 :(得分:0)

请尝试以下代码:

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

答案 1 :(得分: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