Save the listing of files in a directory to a textfile

5 ビュー (過去 30 日間)
Marty Dutch
Marty Dutch 2014 年 4 月 29 日
回答済み: NARESH BILONIYA 2017 年 10 月 27 日
Dear experts,
Im relative new to matlab. I want to save the contains of a directory into a textfile. In linux this is quite easy and goes like this:
ls > filenames.txt
I have the following code already (which works):
runsDir = dir(fullfile(sourceDir,geneDir,dataDir,'/')); %# Get the data for the current directory
dirIndex = [runsDir.isdir]; %# Find the index for directories
fileList = {runsDir(~dirIndex).name}';
Now I have a construct cell array in my workspace (containing string variables with the names of the files). Now I want to save this into a text file. I searched the forum but did not find anything that worked for me.
Many thanks in advance!
Marty

採用された回答

Sara
Sara 2014 年 4 月 29 日
fid = fopen('myfile.txt','w');
for i = 1:length(fileList)
fprintf(fid,'%s\n',fileList{i});
end
fclose(fid);
  2 件のコメント
Sean de Wolski
Sean de Wolski 2014 年 4 月 29 日
If you really want to get fancy, the for-loop can be skipped with comma-separated list expansion of cell arrays.
fprintf('%s\n',fileList{:})
Marty Dutch
Marty Dutch 2014 年 4 月 29 日
thanks! your suggestions worked, although you forgot to put "fid" in the code (see below).
fprintf(fid,'%s\n',fileList{:});

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

その他の回答 (1 件)

NARESH BILONIYA
NARESH BILONIYA 2017 年 10 月 27 日
what is fileList here

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by