Writing data in file and naming file with a string value

1 次查看(过去 30 天)
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Report Generator 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by