x,y coordinates of text
6 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Rik
2021-5-11
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 个评论
Rik
2021-5-11
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.
更多回答(1 个)
KSSV
2021-5-11
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')
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Language Support 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!