2

I'm developing a piece of k-means fuzzy code. Now I want to save the data of each iteration that is displayed by statset('Display','iter');. Help me please.

X = [randn(20,2)+ones(20,2); randn(20,2)-ones(20,2)];
opts = statset('Display','iter');

[cidx, ctrs] = kmeans(X, 2, 'Distance','city', ...
    'Replicates',5, 'Options',opts);

plot(X(cidx==1,1),X(cidx==1,2),'r.', ...
X(cidx==2,1),X(cidx==2,2),'b.', ctrs(:,1),ctrs(:,2),'kx');
Acorbe
  • 8,367
  • 5
  • 37
  • 66

2 Answers2

4

A dummy solution is given by the function diary which enables the storing of the matlab console output on a file.

X = [randn(20,2)+ones(20,2); randn(20,2)-ones(20,2)];
opts = statset('Display','iter');

diary('output.txt')  % # Whatever is displayed from now on is saved on 'output.txt'

[cidx, ctrs] = kmeans(X, 2, 'Distance','city', ...
    'Replicates',5, 'Options',opts);

diary('off')         % # logging is disabled

After the execution, output.txt will contain

 iter    phase       num             sum
  1      1        40          96.442
  2      1         8         79.7403
  3      1         6         70.2776
 ...

You may want to clean the content of output.txt at every run, otherwise it will just append the new log after the previous one.

Acorbe
  • 8,367
  • 5
  • 37
  • 66
0

One way to achieve would be to redirect the output displayed in the MATLAB terminal to a file, say 'my_file.txt'. To do so:

  1. Use the command edit statset.m; to open the file in the MATLAB editor.
  2. Open the output file you require with fid = fopen('my_file.txt','w');.
  3. Replace fprintf( with fprintf(fid,.

You might want to keep a backup copy of statset.m too, just to be on the safe side.

EDIT

@Acorbe's solution gives the same result without all the above hassle.

Roney Michael
  • 3,964
  • 5
  • 30
  • 45