views:

380

answers:

3

I have a 2 column matrix(called M, which I visualize as two vectors using Matlab's plot command(plot(M)). I have two issues:

  1. I want to label the vectors themselves on the plot.
  2. I want to label each row of the matrix(i.e. each vector component) on the plot.

How would I go about doing those things?

A: 

You can label each axis with the function:

xlabel('label')
ylabel('label')

These can also take cell arguments, where each row is a new line. That's handy for showing units. Labeling each point on the figure can be done as so:

for i=1:length(M)
    text(M(i,1),M(i,2),'Label Text')
end

The label text can also be a string variable that you can edit with sprintf and make special strings for each point.

Jeff
It's not the axes I want to label; it's the vectors themselves.
Mike
So you're not plotting M(:,1) vs M(:,2), but rather each column against length(M(:,1)) ? If that's the case, then you can use either the commands: text(M(1,1),M(1,1),'Label 1'),text(M(1,2),M(1,2),'Label 2')or annotation() or legend('Label 1','Label 2')The manual pages have a few examples for this. Text is used the same as before, but for only one point on each line. The annotation function is a lot more useful, but it has lots of options and style capabilities. I threw legend in there as well if you don't want to clutter the plot window.
Jeff
+1  A: 

You may need to tweak this to get the positions of the labels exactly how you want them, but something like this will do the trick.

M = [1 2; 3 4; 5 6]
plot(M)
nrows = size(M, 1);
ncols = size(M, 2);
x = repmat(nrows - .3, 1, ncols);
y = M(end, :) - .3;
labels = cellstr([repmat('Col', ncols, 1), num2str((1:ncols)')]);
text(x, y, labels)
Richie Cotton
+3  A: 
M = rand(10,2);
x = 1:size(M,1);
plot(x, M(:,1), 'b.-', x, M(:,2), 'g.-')
legend('M1', 'M2')
for i=x
    text(i+0.1, M(i,1), sprintf('%.2f', M(i,1)), 'fontsize',7, 'color','b' );
    text(i+0.1, M(i,2), sprintf('%.2f', M(i,2)), 'fontsize',7, 'color','g' );
end

alt text

Alternatively, you can use:

datacursormode()

which will enable the user to just point and click on points to see the data label

Amro

related questions