MATLAB: How to save ‘display-iter’ from fmincon to a .txt file

fminconoptimisationtext file

In the options of a fmincon function I set Display to iter and this is how the output loos like. I would like to save it to a .txt file in the very same form that is displayed on a screen. I googleded it and Someone mentioned that the similar problems might be done by using 'OutputFcn' but I have no idea how to do it as I am not sure how my output function should look like.
Thank you in advance!
No active inequalities. Elapsed time is 0.203401 seconds.
Max Line search Directional First-order
Iter F-count f(x) constraint steplength derivative optimality Procedure
0 5 2496.66 0
1 13 2496.25 0 0.125 -14.5 26.9
2 20 2495.99 -0.1969 0.25 -2.45 21.8
3 26 2495.24 -0.2492 0.5 -2.74 29.9
4 33 2494.38 -0.3498 0.25 -7.68 11.1
5 38 2494.17 -0.2437 1 -11.2 10.8
6 43 2493.36 -0.283 1 -4.19 3.59
7 48 2492.84 -0.162 1 -1.31 9.38
8 53 2492.63 0 1 -2.28 4.2
9 58 2492.61 0 1 -1.28 0.21
10 63 2492.61 0 1 -0.0412 0.111
11 68 2492.57 0 1 -0.0386 2.94 Hessian modified
12 73 2492.52 0 1 -0.0279 5.33
13 78 2492.35 0 1 -0.0271 10.2
14 83 2492 0 1 -0.025 15.5
15 88 2491.35 0 1 -0.0244 18.5
16 93 2490.79 0 1 -0.0234 11.4
17 98 2490.64 0 1 -0.0642 3.4
18 103 2490.61 0 1 -0.00727 0.151
19 108 2490.61 0 1 -0.00048 0.00781

Best Answer

Getting back to the original question about capturing the output to a text file, I would simply use 'diary' instead of an OutputFcn. But that's just me; I like to keep it simple when I can. :)