How do I generate a listbox that contains a list of all the files in a directory?

2 次查看(过去 30 天)
How do I generate a listbox that contains a list of all the files in a directory? How can I write a callback so that I can load a selected file?

采纳的回答

MathWorks Support Team
In order to create a listbox with the names of files, you first need to create the list:
list = dir('*.m');
This returns a structure in the variable "list". In order to extract the filenames from this structure you should do the following:
filelist = {list.name};
The following can be used to create a listbox containing these filenames:
h = uicontrol('Style','listbox','Units','Normalized',...
'Position',[.1 .1 .4 .8],'String',filelist)
In order to load a selected file, you can use either the callback of the ListBox or create another uicontrol to load the selected file. Here is an example of how to write the CallBack for the ListBox we already created:
set(h,'Callback',['s=get(gco,''string'');' ...
'v=get(gco,''value'');open(s{v,:})']);
In addition, if you are using GUIDE, use the CallBack editor to assign the CreateFcn of the listbox:
list = dir('*.m');
str = {list.name};
set(gcbo,'str',str)
There are also MATLAB Central file submissions that show how ths can be done:
<http://www.mathworks.com/matlabcentral/fileexchange/10867-uipickfiles-uigetfile-on-steroids>
Note that MathWorks does not control the content posted by visitors to MATLAB Central, and does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will MathWorks be liable in any way for any content not authored by MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted, or otherwise made available via
MATLAB Central.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

尚未输入任何标签。

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by