โชว์ว่าโปรแกรมมันคำนวณถึงไหนแล้วด้วย ProgressIndicator

ถ้าอยากรู้ว่าโปรแกรมที่เขียนมันคำนวณใกล้เสร็จแล้วยัง ลองประยุกต์ใช้ ProgressIndicator (สำหรับ Mathematica 6.0 ขึ้นไป) ดูครับ

รูปแบบคำสั่ง ProgressIndicator ที่จะแนะนำคือ

ProgressIndicator[ Dynamic[x], {xmin, xmax}]

โดย x คือตัวแปรที่เราใช้ควบคุมความยาวของแถบ indicator
xmin,xmax คือค่าเริ่มต้นกับค่าท้ายสุดของ x จากการรันโปรแกรมของเราครับ

การเอาไปใช้ เช่น ถ้าเรามีโปรแกรมที่คำนวณชื่อ LongLongRun[runsteps_Integer] ที่มีรูปแบบตามนี้

LongLongRun[runsteps_Integer]:=Module[{index,tmp},
For[index=1,index<=runsteps,index++,
Pause[1];
];
tmp=”Finished !”;
tmp
];

เราสามารถประยุกต์ใช้ ProgressIndicator ได้ด้วยการประกาศตัวแปรสำหรับควบคุมความยาวของแถบ indicator ขึ้นมา เช่นชื่อ progress = 0; แล้วให้ค่ามันเท่ากับ index ที่ใช้ใน for loop ดังนั้น โปรแกรม LongLongRun จะมีบรรทัด progress = index; เพิ่มเข้าไป

LongLongRun[runsteps_Integer]:=Module[{index,tmp},
For[index=1,index<=runsteps,index++,
progress=index;
Pause[1];
];
tmp=”Finished !”;
tmp
];

ทดลองrun

%d bloggers like this: