I just want to make a script which runs multiple files and stores their outputs in separate log files.
So I wrote the code by using diary
to generate the output, but the diary
function is only 1 log file and the output is updating in the same log file for remaining iterations. In my testconfig_1
file, I have at present only print as the content.
Then I tried to use the fopen
method, with this I am getting multiple log files but I don't understand how I can put that data into the log file which I have created through fopen
after each run.
% with diary method
clear all;
diary on;
instring ='testconfig_';
for x = 1:3
fprintf ('Simulation on testconfig_%d \n' , x);
test = [instring num2str(x)];
run(test);
diary testconfig_(x).log;
end
% without diary method
clear all;
diary on;
instring ='testconfig_';
for x = 1:3
fprintf ('Simulation on testconfig_%d \n', x);
test = [instring num2str(x)];
run(test);
fid = fopen(sprintf('testconfig_%d.log',x),'w');
end
I wanted to get testconfig_1.log
, testconfig_2.log
, testconfig_3.log
and I wanted to print in_testconfig_1
, in_testconfig_2
, in_testconfig_3
respectively
You are using command syntax to call the diary
function.
% Command syntax
diary filename.log
% Equivalent function syntax
diary( 'filename.log' );
Notice that, when using command syntax, all arguments are treated as strings, despite not having any quote marks!
So when you do diary testconfig_(x).log
, the equivalent is
diary( 'diary testconfig_(x).log' );
All of your logs have the same filename, because x
is never evaluated as the loop variable, it's always just part of a string!
You are trying to create the strings with the loop variable in the name, for this you cannot use command syntax.
Instead, use function syntax like this:
filename = sprintf( 'diary testconfig_%d.log', x );
diary( filename ); % treat the filename variable as a variable, not a string
You don't have to declare the intermediate filename
variable, and there are other ways to create the filename
string, but I hope this demonstrates the issue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.