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

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: http://www.mathworks.com/matlabcentral/fileexchange/13680-traveling-salesman-problem-genetic-algorithm) 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.

Nicolas

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

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...

us

```Hi,

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

- the function has this form :

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

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

Thanks much!
```

```Hi,

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];
end;

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).

Thanks!
```

```Hi,

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
fprintf(fid,'%d\n',meanabs)

This is my code.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++
%function
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
res=rex(:,i);

% to find Mean Absolute Value
tot = sum(abs(res));
meanabs = tot/200;
fprintf(fid,'%d\n',meanabs);

end

fclose(fid);

+++++++++++++++++++++++++++++++++++++++++++++++++++++

```

```Hi,

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.

Mutang
```