MATLAB Answers

x,y coordinates of text

4 views (last 30 days)
v k
v k on 11 May 2021
Commented: v k on 11 May 2021
If we print the following sentence
Lorem ipsum dolor sit amet, consectetur adipiscing elit,
onto the matlab figure, using normalized text options from [0,1],
text(0.1,0.2,'Lorem ipsum dolor sit amet, consectetur adipiscing elit,')
then assuming that the numWords variable containing the total number of words in the sentence is available, how can we find the (x,y) coordinates of the i-th word?
The figure is attached. Objective is to get the starting coordinates of 'sit', 'consectetur', 'elit' etc.
The punctuations are counted as part of the word to their left and are not an issue - they can appear along with the i-th word. Example, word number 5 is actually 'amet,'. This is not important for the starting coordinates of the word, but important if it is possible to get the end-coordinates as well of the i-th word.

Accepted Answer

Rik on 11 May 2021
You can use the Extent property of the text object to find the corners of your text area. The only thing left to do for you is to crop your text to find out how the width is changed by removing each word. You could also do it letter by letter.
axis([0 1 0 1])
t=text(0.1,0.2,'Lorem ipsum dolor sit amet, consectetur adipiscing elit,');
for n=numel(full_str):-1:1
%remove last letter
%add start and width
%restore original text
hold on
Now you have the location of each letter you can use the normal tools (like strfind) to find the relevant indices of your coordinate vector.
% subtract 1 to find the previous letter end, i.e. the start of this word
v k
v k on 11 May 2021

Sign in to comment.

More Answers (1)

KSSV on 11 May 2021
You can use functions like strfind to get the position of the required string from the main string.
str = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,' ;
idx = strfind(str,'sit')
  1 Comment
v k
v k on 11 May 2021
Thanks. But it cannot be explicit word-based. Only by wordNumber. For example, wordNumber 3 starts at (x3s,y3s) and ends at (x3e,y3e).
Also, finding the index is not one of the criterion. idx will not give the coordinates.

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!