how to write in the text 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.

 採用された回答

Thorsten
Thorsten 2015 年 10 月 20 日

0 投票

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)

3 件のコメント

Lilya
Lilya 2015 年 10 月 20 日
It worked, but it does not count the other months only the first one from each year
Thorsten
Thorsten 2015 年 10 月 20 日
If you want filenames for 12 month for each year, use
data = [reshape(repmat(yr, [12 1]), 1, []); ...
repmat(mn, 1, numel(yr))];
Lilya
Lilya 2015 年 10 月 20 日
OMG! Thank you like huge universe

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

その他の回答 (1 件)

Stalin Samuel
Stalin Samuel 2015 年 10 月 20 日

0 投票

clear
FN1 = 'JDA';
z = '0',
mn = 1:12;
yr = 9:13;
ext = '.ohh';
filename = fopen('JDAraw.txt','w');
for k = 1:numel(yr);
yr1 = yr(k);
if yr1==9
tmp = sprintf('%s%s%03d%02d%s',FN1,z,yr1,mn(k),ext)
fid = fopen(tmp,'rt');
end
tmp = sprintf('%s%03d%02d%s',FN1,yr1,mn(k),ext)
fid = fopen(tmp,'rt');
fprintf(filename, '%s\n',tmp);
end

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

質問済み:

2015 年 10 月 20 日

コメント済み:

2015 年 10 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by