(R2016b:Matlab版) 我想将inputdlg中的用户输入作为字符串,然后将所有输入字符串的数组的单元格转换为一个变量
我的代码是:
prompt = {'Input the number of Criterion','Input short name of criterion'};
dlg_title = 'Alternative Evauation';
num_lines = 1;
defaultans = {'3','{Criterion1,Criterion2,Criterion3}'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
虽然我能够将number of Criterion
视为
n=str2num(answer{1})
但是当我尝试将short name of criteria
读为
str=answer{2}
然后不要将str
作为数组的单元格来读取
'Criterion1', 'Criterion2', 'Criterion3'
单独显示为Criterion1Criterion2Criterion3
我希望变量str
为
str={'Criterion1', 'Criterion2', 'Criterion3'...}
输入number of criterion
没有限制,可能是5或7,具体取决于用户,因此相应name of criterion
会增加
答案 0 :(得分:1)
问题是inputdlg
为每个输入变量返回单个字符串。您可以使用strsplit
将此字符串拆分为多个条件名称:
prompt = {'Input the number of Criterion','Input short name of criterion'};
dlg_title = 'Alternative Evauation';
num_lines = 1;
defaultans = {'3','Criterion1,Criterion2,Criterion3'};
answer = inputdlg(prompt,dlg_title,num_lines,defaultans);
% get number of criterions
n = str2num(answer{1});
% get criterion default names (cell array of size [1 n])
defStr = cellfun(@(name,num) [name num2str(num)],...
repmat({'Criterion'},[1 n]),num2cell(1:n),'UniformOutput',0);
% get user supplied criterion names and split by commas
temp = strsplit(answer{2},',');
str = defStr;
% assign user's names instead of default ones
str(1:numel(temp)) = temp;