在程序运行过程中,很多时候需要查看运行结果和中间过程,可以用写入txt或者excel文件中。但对于有很多中间过程的情况存在效率不高或者很多中间过程记录困难的问题。我发现了diary这个命令可以很好的解决这个问题。diary这个语句可以以文本文件的形式记录command window 这个窗口显示的运行结果全部记录下来,便于以后的查看。
比如要记录一下运行信息:
Optimize a model with 40 rows, 16 columns and 116 nonzeros
Coefficient statistics:
Matrix range [1e+00, 4e+01]
Objective range [2e+00, 1e+01]
Bounds range [1e+00, 1e+00]
RHS range [1e+00, 4e+01]
Found heuristic solution: objective 718
Presolve removed 20 rows and 4 columns
Presolve time: 0.00s
Presolved: 20 rows, 12 columns, 96 nonzeros
Variable types: 0 continuous, 12 integer (6 binary)
Root relaxation: objective 4.580000e+02, 9 iterations, 0.00 seconds
Nodes | Current Node | Objective Bounds | Work
Expl Unexpl | Obj Depth IntInf | Incumbent BestBd Gap | It/Node Time
0 0 458.00000 0 5 718.00000 458.00000 36.2% - 0s
0 0 458.00000 0 6 718.00000 458.00000 36.2% - 0s
0 0 458.00000 0 5 718.00000 458.00000 36.2% - 0s
H 0 0 652.0000000 458.00000 29.8% - 0s
0 0 458.00000 0 5 652.00000 458.00000 29.8% - 0s
0 0 459.09091 0 7 652.00000 459.09091 29.6% - 0s
0 0 459.32000 0 7 652.00000 459.32000 29.6% - 0s
0 0 459.80000 0 7 652.00000 459.80000 29.5% - 0s
0 0 459.80000 0 7 652.00000 459.80000 29.5% - 0s
0 0 459.80000 0 7 652.00000 459.80000 29.5% - 0s
0 2 459.80000 0 7 652.00000 459.80000 29.5% - 0s
* 12 0 5 638.0000000 464.54348 27.2% 4.3 0s
Cutting planes:
Gomory: 5
MIR: 9
Explored 45 nodes (192 simplex iterations) in 0.03 seconds
Thread count was 4 (of 4 available processors)
Optimal solution found (tolerance 1.00e-04)
Best objective 6.380000000000e+02, best bound 6.380000000000e+02, gap 0.0%
cpu_time =
0.0360
a =
0 1 0 0
NaN 0 0 0
NaN NaN 0 1
NaN NaN NaN 0
obj =
638
可以有两种方式使用diary:
1,直接在运行的matlab文件夹下生成diary文件保存信息,语句如下:
diary on;
……
diary off;
2,将信息保存在指定的txt文件下,语句如下:
diary('E:\Writing paper\test\Diary.txt'); %The file path and file name.
diary on;
……
diary off;
多次运行程序,结果会逐一保存到文件中。