Eddie
Eddie

Reputation: 119

TreePlot String Labeling MATLAB

nodes = [0 1 2 3 4 4 3 7 7 2 10 11 11 10 14 14 1 17 18 19 19 18 22 22 17 25 26 26 25 29 29]; This is the standard plot for a depth 4 tree. It is in a loop, and gets drawn 10 times. Now, each of these 10 times some numerical computations are done and different numbers are come up with. These numbers all point to some word tags in a main array. Each time these numbers change, the words indexed also change, and I already know how they are to be placed in the tree. How do I label the tree with these strings then ?

I guess, the general question is how to label a tree with a bunch of strings?

Upvotes: 1

Views: 2053

Answers (1)

George Skoptsov
George Skoptsov

Reputation: 3971

A bit of a hack is to look at plotted points and, assuming they have a 1-1 correspondence with the nodes in your vector, use their coordinates to plot text.

treeplot([0 1 1]);  % plot your tree
c = get(gca, 'Children'); % get handles to children
% grab X and Y coords from the second child (the first one is axes)
x = get(c(2), 'XData');
y = get(c(2), 'YData');

Now you can plot whatever at these coordinates. If labels is a cell array of labels, then you can display them next to the nodes as follows:

text(x, y, labels, 'VerticalAlignment','bottom', ...
                         'HorizontalAlignment','right')

Upvotes: 1

Related Questions