Save the listing of files in a directory to a textfile
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Marty Dutch
el 29 de Abr. de 2014
Respondida: NARESH BILONIYA
el 27 de Oct. de 2017
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
0 comentarios
Respuesta aceptada
Sara
el 29 de Abr. de 2014
fid = fopen('myfile.txt','w');
for i = 1:length(fileList)
fprintf(fid,'%s\n',fileList{i});
end
fclose(fid);
2 comentarios
Sean de Wolski
el 29 de Abr. de 2014
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{:})
Más respuestas (1)
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!