x,y coordinates of text

10 Ansichten (letzte 30 Tage)
v k
v k am 11 Mai 2021
Kommentiert: v k am 11 Mai 2021
Hello,
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.
Thanks.

Akzeptierte Antwort

Rik
Rik am 11 Mai 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,');
full_str=t.String;letter_end_loc=zeros(size(full_str));
for n=numel(full_str):-1:1
%remove last letter
t.String=full_str(1:n);
%add start and width
letter_end_loc(n)=t.Extent(1)+t.Extent(3);
end
%restore original text
t.String=full_str;
hold on
plot(letter_end_loc,0.1*ones(size(letter_end_loc)),'*')
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
xline(letter_end_loc(strfind(full_str,'sit')-1))
  3 Kommentare
Rik
Rik am 11 Mai 2021
You need to find the starting index of the 4th word in your char array. That is a completely separate task. A regular expression (doc regexp) will probably be the best choice to generate the list of word start and end indices.
v k
v k am 11 Mai 2021
Thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

KSSV
KSSV am 11 Mai 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 Kommentar
v k
v k am 11 Mai 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.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by