x,y coordinates of text
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Akzeptierte Antwort
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
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.
Weitere Antworten (1)
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')
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!