2017-08-09 37 views
1

通過使用listdlg可以從列表中選擇文件,但它將相應的索引作爲輸出而不是名稱(所選實體的字符串)返回,的選擇。如何在輸出中獲得所選文件的名稱?'如何使用索引值檢索相應的名稱(字符串)

例如

[Selection, ok] = listdlg(Name,Value,...); 

% selection is nothing but a index of selected entities. 
+0

我已經用示例更新了我的帖子。 –

回答

2

該對話框中填充有作爲ListString參數的值設置的單元陣列。致電listdlg的結果是此cellarray的索引。

考慮下面的代碼:

filelist=dir("/home"); 
S={filelist.name}; 
[Selection,ok]=listdlg('ListString',S,'SelectionMode','single'); 
if (ok) filename=cell2mat(S(Selection)) endif 

如果項目user1的選擇是做它應該輸出

filename = user1 

更新

SelectionModemultiple,你可以使用celldisp(S(Selection))。要提取單個項目,請使用S{Selection(i)},其中i的範圍是1到length(Selection)

filelist=dir("/home"); 
S={filelist.name}; 
[Selection,ok]=listdlg('ListString',S,'SelectionMode','multiple'); 
if (ok) 
    for i=1:length(Selection) 
    disp(S{Selection(i)}) 
    end 
endif