MATLAB: Create Text file containing list of file names

cell array into text filecreating txt filelist of file names

I am sure this has been answered before somewhere, but I am getting really frustrated trying to find the correct code.
I have over 400 files in a directory. I would like to create a text file that lists all of their file names. Then place this text file into the directory with the original files, not the matlab working directory. I have to do this well over 100 times for 100 different directories.
The only code that has worked partially is this:
dirName = 'D:\ABN tdump files\ERA -I 10m\April 10m'; %# folder path
files = dir( fullfile(dirName,'*.') ); %# list all *.xyz files
files = {files.name}'; %'# file names
I have created a cell with all of my file names, but now I can't create the text file. I get the very ambiguous error message that just says there is an error with one of my lines of code. Not helpful. So. Question. How does one create a text file with a cell array?

Best Answer

Simpler:
D = 'D:\ABN tdump files\ERA -I 10m\April 10m';
S = dir(fullfile(D,'*.xyz')); % specify the file extension to exclude directories
[fid,msg] = fopen(fullfile(D,'text.txt'));
assert(fid>=3,msg)
fprintf(fid,'%s\n',S.name)
fclose(fid);
Repeat for each directory D.