Writing data in file and naming file with a string value

1 回表示 (過去 30 日間)
RS
RS 2015 年 2 月 18 日
コメント済み: RS 2015 年 2 月 19 日
Hi..
My simulations are finding errors for 4 cases in a loop as shown below. I am writing those errors in data file.
Problem is naming of the file. I want to name my files as error_200.txt, error_400.txt, error_800.txt and error_1600.txt respectively for each case. For that i have to mention the 'n' which contains different values in each loop, in the file name.
something like :fileID = fopen('error_'n'.txt','a') so that file will be automatically generated with the value of n in the name. Here is the code.............
for i = 1:4,
k = 2^i ;
n = k*100
for step = 1:1000
% here some calculations happen and data is generated,
% and i write those date (error) in a file.
error = n % for example
D = [step; error;];
fileID = fopen('error_'n'.txt','a');
fprintf(fileID,'%6.2f %12.4f\n',D);
fclose(fileID);
end
end
Currently it writes all the data in single file. Pl help me modify the code to include the value of 'n' filename while writing the data so that 4 different file are generated with different names.
Thanks.

採用された回答

Yoav Livneh
Yoav Livneh 2015 年 2 月 18 日
Change your fopen line to:
fileID = fopen(['error_', num2str(n), '.txt'],'a');
  4 件のコメント
Yoav Livneh
Yoav Livneh 2015 年 2 月 18 日
Just use
figure(n);
plot(step,error);
or just
figure;
plot(step,error);
The first option will always use the same four figures and overwrite the previous plots, the second option will always create new figures.
RS
RS 2015 年 2 月 19 日
Thanks.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB Report Generator についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by