在程序運行過程中,很多時候需要查看運行結果和中間過程,可以用寫入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;
多次運行程序,結果會逐一保存到文件中。