MATLAB >> How to save every result of a variable in a loop?

by Nicolas Gutl » Thu, 31 Dec 2009 02:34:04 GMT

Hi everyone,

I'm new to Matlab, so my question should be easy for you guys!

I'm using the "Traveling Salesman Problem - Genetic Algorithm" from Joseph Kirk (url: and it works nicely.

Unfortunately, the program only gives me the final result in the workspace, which is "the shortest route for the salesman to travel to each city exactly once and return to the starting city".

What I would like is to have a list of all values that are successively taken by variables of the problem, I mean the value of each variable for each iteration, and for each population member (in the Genetic Algorithm).

So I wonder if there is a "log file" in Matlab that contains the values taken by the variables, or that shows calculations done by the Matlab software.

Do I need to use the Report Generator for doing this ?

I hope my request is clear.

Many thanks for your help,


MATLAB >> How to save every result of a variable in a loop?

by us » Thu, 31 Dec 2009 03:43:05 GMT

one of the solutions
- edit the source and add code, which saves/displays those temp results...
- then
- return them in a new output argument...
- or save them just before you leave the main function...


Similar Threads

1. using a function in a loop and saving the output for every iteration


I am using a function to calculate the velocities given distances and

- the function has this form :

function [t, hvel, whichderiv] = derivative_kp(t, h_parm, whichderiv,

where t = time
h_parm = distances
whichderiv = level of derivative
hvel = velocity

Both t and h_parm are 2-d arrays (R x C) - what is a good way to use
this function in a loop in that case?

When I used a for loop for this function, all values for hvel in the
output get overwritten for every iteration ...

I want to save the value from hvel for every iteration to result in a
2-d array with the same dimensions as h_parm (R x C). Could someone
please help?

Thanks much!

2. Assigning different values to a variable in every loop - MATLAB

3. saving loop results


I have a coded a loop in which the final "results" are a 
row of numbers that are related to "j":

for j = 1:N,
PictureName2 = PictureName (j);
X_Coordinate2 = X_Coordinate (j);
results = [PictureName2, j, X_Coordinate2];

I would like to be able to output one Excel file that has N 
rows (i.e., one row represents each loop through the code) 
with three columns corresponding to the three values 
in "results". 

Is this possible to do? (If I just save the results at the 
end of this code, I only save the final run through the 
loop when j=N).


4. saving multiple results in a loop - MATLAB

5. Save results ( in a loop) to a file


I've tried example using fprintf command (given in this forum) & it worked.  But when I tried with my code, this message came out 

??? Error using ==> fprintf
Invalid file identifier -1.

Error in ==> testsave at 16

This is my code.

function stat = simpstat (res)

%Look at the data from this file
rex = embuffer();

%open file
fid = fopen('c:\test.mat','wt');

%for loop
for i=1:1000

% to find Mean Absolute Value 
tot = sum(abs(res));
meanabs = tot/200;




please advice.

6. Saving results within a loop - MATLAB

7. How to save each iteration result of fminsearch into a txt file or a variable


  If we set Display option to be 'iter',  fminsearch will display output at each iteration. But when I have a lot of calls to fminsearch, I want to automatically save each iteration output into a file or variable for late checkup after the program stops, but it seems that the output of each iteration can only be displayed in the main window.  

For example, we can have the following result displayed in main window:
 Iteration   Func-count     min f(x)         Procedure
     0            1     1.31656e+008         
     1            2     1.31656e+008         initial simplex
     2            4     1.24762e+008         contract inside
     3            6       6.619e+007         contract inside
     4            8       6.619e+007         contract inside
     5           10       6.619e+007         contract inside
but how can I automatically save this output into a file or variable?

Do you know how to save the result of each iteration but not display it in the main window? Thanks a lot for your advice in advance.


8. How to save the imwrite result image in a variable - MATLAB