MATLAB: How to write in the text file

fprinttext file

hi all, the following code is for writing all of data file names into text file, the problem is the name are not written in the text sequentially
clear
FN1 = 'JDA';
z = '0',
mn = 1:12;
yr = 9:13;
ext = '.ohh';
for k = 1:numel(yr);
if yr==9
tmp = sprintf('%s%s%03d%02d%s',FN1,z,yr(k),mn(k),ext)
fid = fopen(tmp,'rt');
end
tmp = sprintf('%s%03d%02d%s',FN1,yr(k),mn(k),ext)
fid = fopen(tmp,'rt');
end
filename = fopen('JDAraw.txt','w');
fprintf(fid,tmp);
If anyone can correct this for me, I'll appreciate the help.

Best Answer

FN1 = 'JDA';
mn = 1:12;
yr = 9:13;
ext = '.ohh';
format = sprintf('%s%%03d%%02d%s\n', FN1, ext);
data = [yr; mn(1:numel(yr))];
text = sprintf(format, data(:))
fid = fopen('JDAraw.txt','w');
fprintf(fid, '%s', text)
fclose(fid)