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

1 回表示 (過去 30 日間)
ZigzS
ZigzS 2018 年 1 月 26 日
コメント済み: ZigzS 2018 年 1 月 26 日
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.
  1 件のコメント
ZigzS
ZigzS 2018 年 1 月 26 日
Thank you! Worked great. I run Ubuntu for the record.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by