how to use listdlg

조회 수: 36 (최근 30일)
Andy
Andy 2011년 9월 30일
How do i use listdlg? I read the documentation, it said
listdlg('ListString',S), what is ListString and what is S?
i wish to make it so that matlab grabs all the names of the files from a directory and stick them into the listbox. Thanks

답변 (3개)

Walter Roberson
Walter Roberson 2011년 9월 30일
In the above, 'ListString' is a literal string constant. In the context, it indicates that the value that follows is the value for the ListString parameter. According to the documentation, very top of the table of options, the parameter (S) to be supplied for the 'ListString' option is,
'ListString' Cell array of strings that specify the list box items.
To get information about all of the files in the directory, use
fileinfo = dir('*');
fileinfo([fileinfo.isdir]) = [];
The first line gets the directory information. The second line checks the directory information and then removes all of the entries that have to do with directories (folders), since you said you wanted the names of "the files" and some people do not consider folders to be files themselves (even though they actually are, technically speaking.)
Now that you have that, you can construct the cell array of strings, one filename per cell, by invoking,
S = {fileinfo.name};

Wayne King
Wayne King 2011년 9월 30일
d = dir;
str = {d.name};
[s,v] = listdlg('PromptString','Select a file:',...
'SelectionMode','single',...
'ListString',str)
'ListString',S
is what is called a name-value pair. S is cell array of strings of what is listed in your dialog.
  댓글 수: 1
Andy
Andy 2011년 10월 3일
can you give me an example? i did what you did there, but it says
??? Attempt to reference field of non-structure array.
thanks

댓글을 달려면 로그인하십시오.


Wayne King
Wayne King 2011년 10월 3일
You copied and pasted this into the command window? This should work.
d = dir;
str = {d.name};
[s,v] = listdlg('PromptString','Select a file:',...
'SelectionMode','single',...
'ListString',str)
  댓글 수: 3
Andy
Andy 2011년 10월 3일
i am trying to use another directory, so what i got is
d = rootdir;
str = {d.name};
[s,v] = listdlg('PromptString','Select a file:',...
'SelectionMode','single',...
'ListString',str)
and it doesnt work
Walter Roberson
Walter Roberson 2011년 10월 3일
"dir" in Wayne's answer is a call to the dir() function, not the name of a directory.
cd(rootdir);
now follow Wayne's instructions.

댓글을 달려면 로그인하십시오.

카테고리

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