Draw me now…

Finding bugs can be a daunting task sometimes… Today I was stucked with an error that eventually turned out to be due to a very common mistake. To my defense, it was about a mechanism that is not really intuitive on a first look : drawnow.

In this post, I talk about Matlab calculation thread, Java thread and a very common problem : Why does this stupid call to graphic display is not executed in its time?

Let’s suppose you execute the following code :

figure(1);
for i=1:1000
   T(i)=cos(i);
   plot(T);
end

First, I know I don’t preallocate. This is intentional.

This code will provide you with a figure with a cosinus curve in the end but you won’t see the plot “filling up” in front of you.

Some might say : it’s because it is calculated too fast by the computer!

In fact, no, here I intentionaly made my code very very bad. No preallocation and a call to graphics in a for loop. Double terrible. So why don’t we see the plot being slowly filled on the screen?

This is due to some very fundamental properties of how graphics and calculations are organized in Matlab and it is VERY important to understand this point.

For the most part, Matlab runs its calculation on a single calculation thread (it is not exactly true, most matrix calculations are multithreads now but that is hidden to you). BUT I am sure you have noticed that when you run a calculation and a GUI, sometimes the GUIs are still responsive. This is because the graphics in Matlab runs on a different thread, a Java thread called the Event Dispatch Thread (EDT). What all of this means?

The EDT is capable of sort of storing all you calls to graphics in a queue as you creates them BUT the Matlab calculation thread has always the priority so Mathworks allowed the calls in the EDT to be executed at certain precise moments. These moments are creating a new figure, use getframe, when you ask for keyboard input, … and going back to the Matlab prompt at the end of your function (which is what happened in our example). A complete list of these moment is available here.

Most of the time, this will be enough as you only cares of taking a look at this graphics when the calculation is finished. So for most users, they won’t even NOTICE that all of this is happening in the background.

However, sometimes, this is not enough. You WANT this figure to be updated and the EDT to be ‘flushed’. Your calculation is very long or you need it for your interface to behave properly. This is the role of drawnow as in this new version of the code :

figure(1);
for i=1:1000
   T(i)=cos(i);
   plot(T);
   drawnow;
end

Here drawnow forces Matlab to stop its calculation thread to perform the demanded graphic displays. If you run this code, you will “see” the figure window filling up.

Of course, you should be very very conservative with drawnow. Only use it when needed. I am sure you will understand that stopping a calculation thread for display purposes is not going to make your calculation faster.

Sometimes, the play between the EDT and the calculation thread can be very very confusing. You made those calls to graphics but nothing happens and your computer is sort of stuck in a calculation instead of showing your graph. Most of the time your forgot (as I did) the nearby for loop that is capturing all of Matlab attention. Drawnow is to sort of refocus it on you and stop watching its beautifull calculations…

This entry was posted in Advanced, Intermediate. Bookmark the permalink.

7 Responses to Draw me now…

  1. I have been wondering as to why this is and I suspected something similar to what you described here.

    I have a question for a side project of mine that I am currently rewriting because I wrote it as I was learning MATLAB and it is POORLY written (global variables everywhere…*sigh*).

    Essentially I want to show a wave propagate in space as time progresses. Really just a sine wave traveling along the plot for demonstration and teaching purposes of wave mechanics. What would be the best way to “animate” the wave?

    I have considered something along the lines of drawnow but I don’t want it to be a drain on the computer. How could the situation be handled?

    Thanks

    • Jerome says:

      In this particular case, I would use a timer object. I am planning on making a post on this. Check my post on realtime processing for a working example of this.
      Then you create a callback executed by the timer object that changes ONLY the data in the figure. You update the XData and YData property of the axes you want to animate.

      This way, the timer object gives you a nice control on the speed of the display via its refresh rate properties.

  2. [url=http://www.eevance.com/tokei/ap/index.html]経典でCalibre 13R0とCalibre 1315ムーブメントの基礎の上に、ブランパン改善技術、再び自主開発して生産した2種類の高級ムーブメント:めいカレンダーやパワーリザーブ機能のCalibre 13R5、めい月相損益表示と完備カレンダーのCalibre 66R9や、結合したはずみ車の回転機構とグリニッジ陀時間表示機能のCalibre 5025。また、Calibre 1315ムーブメントの「改訂版」、新しいムーブメントを備えCalibre 5215はGMTと5日動力貯蔵機能。スーパーコピーブランドそしてこの何項の革新ムーブメント、それぞれで時計をL-Evolutionシリーズの最終の落ち着き先。[/url]

  3. [url=http://www.brandiwc.com/brand-super-18-copy-0.html]「clé」のための「鍵」と新しい腕時計をあなたの典型的な丸いクラウンの王冠に言及します。その代わりに、カルティエカルティエの時計はclé面白い触覚体験を提供する長い冠を持っています。クラウンとロックの場所になったとき、あなたのような感じは、文字通り、古い時計や懐中時計の重要な巻。「キースタイルブルーサファイアのカボション冠の大部分のカルティエの時計の特徴となっている。[/url]

  4. [url=http://www.gginza.com/%E6%99%82%E8%A8%88/%E3%83%AD%E3%83%AC%E3%83%83%E3%82%AF%E3%82%B9/daytona/65198499868fb2b5.html]足跡を踏破する世界のティソは、1853年、タブ要衝スイス力ロック正式の誕生以来、地域の限界を越えて、まぜる優良な伝統と革新のインスピレーション、ブランドの時計を持ってすばらしい作品世界各地まで。2013年に創立160週年を祝ってティソその旅行者精神を分かち合うことは、発売を表示することができる世界24時間のタイムゾーンHeritage Navigator 160週年記念愛蔵版経典世界時間自動時計のほか、特別に開催された「160年环宇漫遊」の計招待試合切手、各界の人々に「旅」をテーマに、創作の切手設計、その小さなに切手をティソ連れの旅行者の精神世界の隅々まで。ブランドコピー時計今回は設計試合に誘われてグロリア-イップさんを判定するほかに、彼女はよりと他の2人審判団メンバー――有名なグラフィックデザイナー余孝扬さんやスイスティソグローバル黄暁明さんと一緒に参加しまし大使切手設計とともに、試合の冠、亜や3位作品と一緒に作った真実のご利用をスイス印ティソ「160年环宇漫遊記念切手」シリーズの証人として、歴史時刻。[/url]

  5. [url=http://www.ooobrand.com/bags/hermes/640.html]もしアリストテレスはまだ生きている、彼はきっとそう評価Blancpainブランパン:“抜群の良好な習慣を訓練としての芸術形式は、したがって、抜群のは1種の行為ではなくて、1種の習慣。」継2006年と2007年にそれぞれ出しCalibre 13R0とCalibre 1315 2項のムーブメントの後、ブランパンは自主開発に取り組んで時計ムーブメントCalibres 13R5、Calibre 66R9とCalibre 5025ムーブメントはCalibres 13R0を基礎にして、Blancpainを表現した新型腕時計開発高い品質ムーブメントの決意を固める。ブランドコピーその執着と信念は、新しいシリーズが全面的に体現L-Evolution。これも証明し、卓越しただけでは、不断の努力の成果。[/url]

  6. [url=http://www.okakaku.com/Super-Copy-9577.html]超人気旺店全新登場人気ブランド品★2016年全新登場★揃っている。ロレックス腕時計、オメガ、ブルガリ、カルティエIWC腕時計、シャネルコピーバッグ、財布腕時計、バネライ、ベル&ロス、ゴルム腕時計チュードル腕時計、ルイヴィトン、グッチバッグ財布、コピーミュウミュウバッグ財布、エルメス、ブラダバッグ、財布等弊社は「信用第一」をモットーにお客様にご満足頂けるよう、送料は無料です(日本全国)! ご注文を期待しています!下記の連絡先までお問い合わせください。是非ご覧ください[/url]

Leave a Reply

Your email address will not be published. Required fields are marked *