Want to define a cell vector according to the files in a directory

2 次查看(过去 30 天)
I want to define a variable (say "files") as a cell array containing the names of the folders in a specific directory. Consider the directory of interest (home/directory) that contains 4 folders named 'AAAA' 'BBB' 'CC' 'D'. The following command yields the following result:
files=ls('/home/directory');
files=AAAABBBCCD
I want files to be a cell array containing 4 elements
files(1) = AAAA
files(2) = BBB
files(3) = CC
files(4) = D
Any ideas? Thanks

采纳的回答

Walter Roberson
Walter Roberson 2018-1-26
dinfo = dir('/home/directory');
files = {dinfo.name};
Note that the directory will not be included as part of what is stored in files
Do not use ls() for this purpose: On OS-X and Linux it invokes the shell ls command, which produces multiple columns per line, space separated, with embedded newline characters. You might be tempted to use that and then to split at whitespace, but if you do that then you will accidentally file names that contain whitespace in them. The dir() that I show will not have problems this way.

更多回答(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