Search code examples
matlabpsychtoolbox

How do i get the bounding rect of each word in a text (for reading eyetracking)?


For an eye tracking study i need the positions of each word in a text drawn to the window. I can see how I can get the bounding box of the whole text as a return value using

[nx, ny, textbounds] = DrawFormattedText(win, tstring)

Is there a better way than drawing a whole sentence using this function word by word?


Solution

  • Something like this should do it:

    teststr = {'Hello World!' ; 'How are you doing?'}
    
    ystart = 100
    xstart = 200
    wordgap = 10
    
    for i=1:size(teststr,1)
    
      str=teststr{i};
      wordlist = strsplit(str , ' ');
    
      for j=1:size(wordlist)(1)
        [nx, ny, textbounds]=DrawFormattedText(win, wordlist{j} ,xstart, ystart);
        poslist{j} = textbounds;
        xstart=nx+wordgap;
      end
    
    end
    

    Not pretty, but it works. You will get problems if you have linebreaks.

    EDIT: 2015-07-14: added wordgap suggestion from sven.io