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

2 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 9 Mar 2010
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.

Più risposte (0)

Categorie

Scopri di più su File Operations in Help Center e File Exchange

Tag

Non è stata ancora inserito alcun tag.

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by