# DrawFormattedText faster?

**URL:** <https://psychtoolbox.discourse.group/t/drawformattedtext-faster/1986>\
**Category:** Uncategorized\
**Created:** [October 6, 2008, 3:59am UTC](https://psychtoolbox.discourse.group/t/drawformattedtext-faster/1986 "2008-10-06T03:59:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [October 6, 2008, 3:59am UTC](https://psychtoolbox.discourse.group/t/drawformattedtext-faster/1986/1 "2008-10-06T03:59:26Z")

</div>

Hello,  
I've been using DrawFormattedText to display text as part of an fMRI  
experiment in which each trial contains a statement that is about 4  
lines long (I have about 100 trials).  
  
Since DrawFormattedText calls Screen once for each line of text (to  
create line breaks), I'm finding that it takes too long to keep  
accurate timing for my experiment (it takes about 5ms for each Screen  
call, so I'm losing about 20ms to display 4 lines of text, and over  
the course of the experiment, I'm off by a few seconds as a result).   
  
I was going to try to draw to an offscreen window first (while the  
previous trial is up), but all of the documentation I've been able to  
find says that you should use MakeTexture instead of  
OpenOffscreenWindow, but I can't figure out how to combine MakeTexture  
with DrawFormattedText.  
  
Any advice re: how to improve the timing here would be greatly  
appreciated.  
Thanks,  
Emily

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [October 6, 2008, 11:14am UTC](https://psychtoolbox.discourse.group/t/drawformattedtext-faster/1986/2 "2008-10-06T11:14:06Z")

</div>

Which operating system? High quality text is  
generally slow to draw. But on MS-Windows,  
you could trade beauty against drawing speed  
by enforcing use of the old, quick & ugly  
text renderer which so many people loved to hate:  
  
At beginning of script:  
Screen('Preference', 'TextRenderer', 0);  
  
Other than that, draw into offscreen windows.  
What the documentation (in a convoluted way)  
means to say is that if you just want to convert some  
images (Matlab matrices) into something, Psychtoolbox  
understands, then you should prefer 'MakeTexture' over  
the old-style sequence 'OpenOffscreenWindow' -\>  
'PutImage'.  
  
'MakeTexture' is optimized for fast image conversion,  
the old way is not.  
  
If you want something offscreen to draw into, then  
of course offscreen windows are the way to go -  
that's what they're meant for.  
  
To make things more interesting, if you open  
your onscreen window like this:  
  
PsychImaging('PrepareConfiguration');  
PsychImaging('AddTask', 'General', 'UseFastOffscreenWindows');  
win = PsychImaging('OpenWindow', ......);  
  
instead of  
  
win = Screen'OpenWindow', ......);  
  
and you have halfway recent graphics hardware,  
then offscreen window support will be faster.  
  
\<[http://docs.psychtoolbox.org/wikka.php?wakka=PsychImaging](http://docs.psychtoolbox.org/wikka.php?wakka=PsychImaging)\>  
  
(or "help PsychImaging") may contain more useful  
stuff for you.  
  
-mario  
  
  

> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "fatlilmonkey" \<emily.falk@...\> wrote:  
> \>  
> \> Hello,  
> \> I've been using DrawFormattedText to display text as part of an fMRI  
> \> experiment in which each trial contains a statement that is about 4  
> \> lines long (I have about 100 trials).  
> \>   
> \> Since DrawFormattedText calls Screen once for each line of text (to  
> \> create line breaks), I'm finding that it takes too long to keep  
> \> accurate timing for my experiment (it takes about 5ms for each Screen  
> \> call, so I'm losing about 20ms to display 4 lines of text, and over  
> \> the course of the experiment, I'm off by a few seconds as a result).   
> \>   
> \> I was going to try to draw to an offscreen window first (while the  
> \> previous trial is up), but all of the documentation I've been able to  
> \> find says that you should use MakeTexture instead of  
> \> OpenOffscreenWindow, but I can't figure out how to combine MakeTexture  
> \> with DrawFormattedText.  
> \>   
> \> Any advice re: how to improve the timing here would be greatly  
> \> appreciated.  
> \> Thanks,  
> \> Emily  
> \>

---

<div class="post-metadata">

**Author:** ![xrli2002](https://avatars.discourse-cdn.com/v4/letter/x/48db29/32.png) [@xrli2002](https://psychtoolbox.discourse.group/u/xrli2002)\
**Post date:** [October 6, 2008, 3:50pm UTC](https://psychtoolbox.discourse.group/t/drawformattedtext-faster/1986/3 "2008-10-06T15:50:52Z")

</div>

It seems to me that you calculate the display time and then wait for   
certain amount of time for each trial. This is not good practically,   
especilly for fMRI. You'd better wait till a certain time point for   
each trial, so the time offset within each trial won't accumulate.   
You can use  
WaitSecs('UntilTime', when)  
or something similar for this purpose. Then the 20 ms delay within   
each trial won't be a problem at all.  
Xiangrui  
  
--- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "fatlilmonkey" \<emily.falk@...\>   
wrote:  

> \>  
> \> Hello,  
> \> I've been using DrawFormattedText to display text as part of an fMRI  
> \> experiment in which each trial contains a statement that is about 4  
> \> lines long (I have about 100 trials).  
> \>   
> \> Since DrawFormattedText calls Screen once for each line of text (to  
> \> create line breaks), I'm finding that it takes too long to keep  
> \> accurate timing for my experiment (it takes about 5ms for each

Screen  

> \> call, so I'm losing about 20ms to display 4 lines of text, and over  
> \> the course of the experiment, I'm off by a few seconds as a

result).   

> \>   
> \> I was going to try to draw to an offscreen window first (while the  
> \> previous trial is up), but all of the documentation I've been able

to  

> \> find says that you should use MakeTexture instead of  
> \> OpenOffscreenWindow, but I can't figure out how to combine

MakeTexture  

> \> with DrawFormattedText.  
> \>   
> \> Any advice re: how to improve the timing here would be greatly  
> \> appreciated.  
> \> Thanks,  
> \> Emily  
> \>

---

<div class="post-metadata">

**Author:** ![Allen\_Ingling](https://avatars.discourse-cdn.com/v4/letter/a/a183cd/32.png) [@Allen\_Ingling](https://psychtoolbox.discourse.group/u/Allen_Ingling)\
**Post date:** [October 7, 2008, 3:22pm UTC](https://psychtoolbox.discourse.group/t/drawformattedtext-faster/1986/4 "2008-10-07T15:22:05Z")

</div>

Thanks Mario.  
I'm using a MacBook with OSX.   
I'll try PsychImaging and see what I can do.  
  
Thanks also to Xiangrui for the timing suggestion. I have thought  
about implementing this in the past, but the structure of my program  
includes a few features that make that more complicated (for example,  
I play movies using a separate function that doesn't give control back  
to the main script until the movie is over, sometimes have pause  
trials that don't have a fixed duration, and need to check for/ record  
key presses throughout the trial).   
  
Regarding the above suggestion, is there something analogous to  
PsychPortAudio for videos (something that starts video playback and  
then gives control back to the main script?).   
-Emily  
  
--- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "Mario Kleiner"  
\<mario.kleiner@...\> wrote:  

> \>  
> \> Which operating system? High quality text is  
> \> generally slow to draw. But on MS-Windows,  
> \> you could trade beauty against drawing speed  
> \> by enforcing use of the old, quick & ugly  
> \> text renderer which so many people loved to hate:  
> \>   
> \> At beginning of script:  
> \> Screen('Preference', 'TextRenderer', 0);  
> \>   
> \> Other than that, draw into offscreen windows.  
> \> What the documentation (in a convoluted way)  
> \> means to say is that if you just want to convert some  
> \> images (Matlab matrices) into something, Psychtoolbox  
> \> understands, then you should prefer 'MakeTexture' over  
> \> the old-style sequence 'OpenOffscreenWindow' -\>  
> \> 'PutImage'.  
> \>   
> \> 'MakeTexture' is optimized for fast image conversion,  
> \> the old way is not.  
> \>   
> \> If you want something offscreen to draw into, then  
> \> of course offscreen windows are the way to go -  
> \> that's what they're meant for.  
> \>   
> \> To make things more interesting, if you open  
> \> your onscreen window like this:  
> \>   
> \> PsychImaging('PrepareConfiguration');  
> \> PsychImaging('AddTask', 'General', 'UseFastOffscreenWindows');  
> \> win = PsychImaging('OpenWindow', ......);  
> \>   
> \> instead of  
> \>   
> \> win = Screen'OpenWindow', ......);  
> \>   
> \> and you have halfway recent graphics hardware,  
> \> then offscreen window support will be faster.  
> \>   
> \> \<[http://docs.psychtoolbox.org/wikka.php?wakka=PsychImaging](http://docs.psychtoolbox.org/wikka.php?wakka=PsychImaging)\>  
> \>   
> \> (or "help PsychImaging") may contain more useful  
> \> stuff for you.  
> \>   
> \> -mario  
> \>   
> \>   
> \> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "fatlilmonkey" \<emily.falk@\> wrote:  
> \> \>  
> \> \> Hello,  
> \> \> I've been using DrawFormattedText to display text as part of an fMRI  
> \> \> experiment in which each trial contains a statement that is about 4  
> \> \> lines long (I have about 100 trials).  
> \> \>   
> \> \> Since DrawFormattedText calls Screen once for each line of text (to  
> \> \> create line breaks), I'm finding that it takes too long to keep  
> \> \> accurate timing for my experiment (it takes about 5ms for each Screen  
> \> \> call, so I'm losing about 20ms to display 4 lines of text, and over  
> \> \> the course of the experiment, I'm off by a few seconds as a result).   
> \> \>   
> \> \> I was going to try to draw to an offscreen window first (while the  
> \> \> previous trial is up), but all of the documentation I've been able to  
> \> \> find says that you should use MakeTexture instead of  
> \> \> OpenOffscreenWindow, but I can't figure out how to combine MakeTexture  
> \> \> with DrawFormattedText.  
> \> \>   
> \> \> Any advice re: how to improve the timing here would be greatly  
> \> \> appreciated.  
> \> \> Thanks,  
> \> \> Emily  
> \> \>  
> \>

---

<div class="post-metadata">

**Author:** ![mariokleiner](https://avatars.discourse-cdn.com/v4/letter/m/13edae/32.png) [@mariokleiner](https://psychtoolbox.discourse.group/u/mariokleiner)\
**Post date:** [November 20, 2008, 7:47pm UTC](https://psychtoolbox.discourse.group/t/drawformattedtext-faster/1986/5 "2008-11-20T19:47:19Z")

</div>

A 'clear PsychImaging' would do the trick as well, but  
this error shouldn't happen at all, and certainly not  
in dependence on different graphics cards or  
operating systems or whatever.  
  
The current state is tracked by a simple   
"persistent configphase\_active" variable in PsychImaging.m  
which is checked and changed in various places. After  
a successfull call to PsychImaging('OpenWindow'), the  
variable is returned to its original value, ie.  
  
PsychImaging('PrepareConfiguration');  
PsychImaging('AddTask', 'General', 'UseFastOffscreenWindows');  
win = PsychImaging('OpenWindow', screen1);  
  
PsychImaging('PrepareConfiguration');  
PsychImaging('AddTask', 'General', 'UseFastOffscreenWindows');  
win = PsychImaging('OpenWindow', screen2);  
  
should work without problems. It's not clear to me  
how this could fail, unless you found a very  
weird bug within Matlab itself?  
  
The variable just tracks/ensures the correct  
required order of calls to the different PsychImaging  
subfunctions.  
  
-mario  
  

> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "Adam Greenberg" \<agreenb@...\> wrote:  
> \>  
> \> As Mario suggests below, using PsychImaging('PrepareConfiguration').... to open  
> \> windows makes offscreen window support much better. I've been using this method  
> \> without problems. Recently, however, I started an experiment where functions are   
> called  
> \> in sequence. Because some are using stereo mode and others not, I have each function  
> \> opening a new set of PsychImaging windows (as below). The problem is that on the   
> \> second function [when PsychImaging('PrepareConfiguration').... is issued the second   
> time],   
> \> I'm getting the following error:  
> \>   
> \> ??? Error using ==\> PsychImaging at 588  
> \> Tried to prepare a new configuration phase, but you did not finalize the previous phase   
> \> yet!   
> \>   
> \> My question is, is there a way to end the PsychImaging pipeline (without doing a 'clear   
> all')   
> \> so that this type of error doesn't occur? Currently, I'm issuing a Screen('CloseAll') at the   
> \> end of each script and that doesn't seem to be killing the imaging pipeline.  
> \>   
> \> This is on an iMac (ATI Radeon HD2600) running OS X 10.4.11 and Matlab 2007a.  
> \>   
> \> Interestingly, I don't get this error on my MacBook Pro (ATI Radeon X1600) with the   
> same   
> \> OS and Matlab versions.  
> \>   
> \> Thanks,  
> \> Adam  
> \>   
> \> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "Mario Kleiner" \<mario.kleiner@\> wrote:  
> \> \> To make things more interesting, if you open  
> \> \> your onscreen window like this:  
> \> \>   
> \> \> PsychImaging('PrepareConfiguration');  
> \> \> PsychImaging('AddTask', 'General', 'UseFastOffscreenWindows');  
> \> \> win = PsychImaging('OpenWindow', ......);  
> \> \>   
> \> \> instead of  
> \> \>   
> \> \> win = Screen'OpenWindow', ......);  
> \> \>   
> \> \> and you have halfway recent graphics hardware,  
> \> \> then offscreen window support will be faster.  
> \> \>   
> \> \> \<[http://docs.psychtoolbox.org/wikka.php?wakka=PsychImaging](http://docs.psychtoolbox.org/wikka.php?wakka=PsychImaging)\>  
> \> \>   
> \> \> (or "help PsychImaging") may contain more useful  
> \> \> stuff for you.  
> \> \>   
> \> \> -mario  
> \>

---

<div class="post-metadata">

**Author:** ![agreenb1](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@agreenb1](https://psychtoolbox.discourse.group/u/agreenb1)\
**Post date:** [November 21, 2008, 4:24pm UTC](https://psychtoolbox.discourse.group/t/drawformattedtext-faster/1986/6 "2008-11-21T16:24:18Z")

</div>

Mario, thanks for your guidance. I figured out the problem; it was a bug  
in my code. Instead of calling 'PsychImaging('OpenWindow'...)', in some cases  
I was calling 'Screen('OpenWindow'...)' instead. I guess we now know what  
to look for when people report the error I was receiving.  
  
Thanks,  
Adam  
  

> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "Mario Kleiner" \<mario.kleiner@...\> wrote:  
> \>  
> \> A 'clear PsychImaging' would do the trick as well, but  
> \> this error shouldn't happen at all, and certainly not  
> \> in dependence on different graphics cards or  
> \> operating systems or whatever.  
> \>   
> \> The current state is tracked by a simple   
> \> "persistent configphase\_active" variable in PsychImaging.m  
> \> which is checked and changed in various places. After  
> \> a successfull call to PsychImaging('OpenWindow'), the  
> \> variable is returned to its original value, ie.  
> \>   
> \> PsychImaging('PrepareConfiguration');  
> \> PsychImaging('AddTask', 'General', 'UseFastOffscreenWindows');  
> \> win = PsychImaging('OpenWindow', screen1);  
> \>   
> \> PsychImaging('PrepareConfiguration');  
> \> PsychImaging('AddTask', 'General', 'UseFastOffscreenWindows');  
> \> win = PsychImaging('OpenWindow', screen2);  
> \>   
> \> should work without problems. It's not clear to me  
> \> how this could fail, unless you found a very  
> \> weird bug within Matlab itself?  
> \>   
> \> The variable just tracks/ensures the correct  
> \> required order of calls to the different PsychImaging  
> \> subfunctions.  
> \>   
> \> -mario  
> \>   
> \> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "Adam Greenberg" \<agreenb@\> wrote:  
> \> \>  
> \> \> As Mario suggests below, using PsychImaging('PrepareConfiguration').... to open  
> \> \> windows makes offscreen window support much better. I've been using this method  
> \> \> without problems. Recently, however, I started an experiment where functions are   
> \> called  
> \> \> in sequence. Because some are using stereo mode and others not, I have each   
> function  
> \> \> opening a new set of PsychImaging windows (as below). The problem is that on the   
> \> \> second function [when PsychImaging('PrepareConfiguration').... is issued the second   
> \> time],   
> \> \> I'm getting the following error:  
> \> \>   
> \> \> ??? Error using ==\> PsychImaging at 588  
> \> \> Tried to prepare a new configuration phase, but you did not finalize the previous   
> phase   
> \> \> yet!   
> \> \>   
> \> \> My question is, is there a way to end the PsychImaging pipeline (without doing a 'clear   
> \> all')   
> \> \> so that this type of error doesn't occur? Currently, I'm issuing a Screen('CloseAll') at   
> the   
> \> \> end of each script and that doesn't seem to be killing the imaging pipeline.  
> \> \>   
> \> \> This is on an iMac (ATI Radeon HD2600) running OS X 10.4.11 and Matlab 2007a.  
> \> \>   
> \> \> Interestingly, I don't get this error on my MacBook Pro (ATI Radeon X1600) with the   
> \> same   
> \> \> OS and Matlab versions.  
> \> \>   
> \> \> Thanks,  
> \> \> Adam  
> \> \>   
> \> \> --- In [psychtoolbox@yahoogroups.com](mailto:psychtoolbox@yahoogroups.com), "Mario Kleiner" \<mario.kleiner@\> wrote:  
> \> \> \> To make things more interesting, if you open  
> \> \> \> your onscreen window like this:  
> \> \> \>   
> \> \> \> PsychImaging('PrepareConfiguration');  
> \> \> \> PsychImaging('AddTask', 'General', 'UseFastOffscreenWindows');  
> \> \> \> win = PsychImaging('OpenWindow', ......);  
> \> \> \>   
> \> \> \> instead of  
> \> \> \>   
> \> \> \> win = Screen'OpenWindow', ......);  
> \> \> \>   
> \> \> \> and you have halfway recent graphics hardware,  
> \> \> \> then offscreen window support will be faster.  
> \> \> \>   
> \> \> \> \<[http://docs.psychtoolbox.org/wikka.php?wakka=PsychImaging](http://docs.psychtoolbox.org/wikka.php?wakka=PsychImaging)\>  
> \> \> \>   
> \> \> \> (or "help PsychImaging") may contain more useful  
> \> \> \> stuff for you.  
> \> \> \>   
> \> \> \> -mario  
> \> \>  
> \>
