如何使用索引值检索相应的名称(字符串)

how an index value can be used to retrieve the respective name(string)

通过使用 listdlg 可以 select 列表中的文件,但它 returns 相应的索引作为输出而不是名称(selected 实体的字符串) select 离子。如何在输出中获得 selected 文件的名称??'

例如

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

% selection is nothing but a index of selected entities. 

对话框中填充了作为 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