MATLAB Answers

Save multiple run result of script

76 views (last 30 days)
yh sf
yh sf on 20 Nov 2017
Edited: Stephen Cobeldick on 1 Jan 2020
Hi i'm writing a code with matlab, i wonder if there is a way to save the result of my script every time i run it.Example if i run this code multiple times i want to get all different outputs in my vector or file in case of using save function.
s=[];
s=fitness(D,vect);
Because every time i execute, it overwrites the last result.
Thank you.

  0 Comments

Sign in to comment.

Accepted Answer

KL
KL on 20 Nov 2017
Edited: KL on 20 Nov 2017
EDITED
numOfRuns = 5;
for n=1:numOfRuns
s = fitness(D,vect);
save(['result_data_run_number_' num2str(n) ',mat'],'s');
end

  5 Comments

Show 2 older comments
yh sf
yh sf on 20 Nov 2017
I fixed it but then i still have the same problem because the loop executes in one run and i get same results
KL
KL on 20 Nov 2017
You will get different results only if you change some of your code. I only showed you how to run a code multiple times and store the outcome in different mat files.
Put the part of the code you want to repeat inside for loop and save should be at the end of the loop.
yh sf
yh sf on 20 Nov 2017
Thank you, my fault, so obvious but i was stuck with the idea that i have to click on the run button every time.

Sign in to comment.

More Answers (1)

Stephen Cobeldick
Stephen Cobeldick on 20 Nov 2017
Edited: Stephen Cobeldick on 1 Jan 2020
An alternative to using a loop: download my FEX submission nextname, which will generate the next name each time you call it. Simply put this in your script to generate the filename:
name = nextname('result','_1','.mat')

  0 Comments

Sign in to comment.

Sign in to answer this question.