2017-04-22 132 views
-1

(R2016b:Matlab的版) 我想在inputdlg作爲字符串的用戶輸入,然後將所有的輸入串的陣列的單元爲一個可變如何在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會增加

+1

你可以嘗試使用['strsplit'(https://nl.mathworks.com/help/matlab/ref/strsplit.html)或者使用兩個輸入對話框:一個用於編號,另一個用於您爲第一個輸入對話框中提供的輸入字段提供儘可能多的實際條件。 – m7913d

回答

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; 
+0

真棒...不僅現在用戶輸入的作品,但你也照顧了默認名稱....非常感謝你! – Sanjeev

相關問題