1

I'm quite new to MATLAB and therefore any help is very appreciated.

I want to visualize multiple vectors using MATLAB's compass graph. Is it possible to colorize the different vectors? Those vectors change over time and the order in which they're handed to the compass graph never changes. Is there another way to distinguish the vectors?

Thank you in advance! M.

EDIT: The solution works (i.e. arrows are colored) until the very last vector element of the compass is reached. If I do not stop the loop before, the method exits with the error ??? Subscript indices must either be real positive integers or logicals.. Every object (i.e. arrows) of the compass is correctly adressed except the last; checked the indexes, everything seems to be ok.What can I do?

Here's the code I currently use:

handle = compass(viewframe(1,:),viewframe(2,:));
colors = get(0,'DefaultAxesColorOrder');
for i=1:length(handle)
   set(handle(i),'color', colors(mod(i,length(colors)),:))
end
Eric
  • 1,594
  • 1
  • 15
  • 29

2 Answers2

4

Building on @cyborg answer, you could assign the colors in one call:

Z = eig(randn(5));
clr = lines(numel(Z));  %# colors you want to use

h = compass(Z);         %# compass(real(Z),imag(Z))
set(h, {'Color'},num2cell(clr,2), 'LineWidth',2)

compass

You could also use a legend for annotation:

str = cellstr( num2str((1:numel(Z))','Arrow %d') );  %'
legend(h, str, 'Location','BestOutside')
Community
  • 1
  • 1
Amro
  • 123,847
  • 25
  • 243
  • 454
  • 1
    @Marcus: ah, the way you are using the MOD function is a bit off: when the loop variable `i` is equal to a multiple of `length(colors)`, the remainder is `0` thus the invalid subscript error... I fixed cyborg's answer – Amro Oct 31 '11 at 22:58
  • Thanks @Amro, I always forget that Matlab indices are 1-based. – Eric Nov 01 '11 at 07:30
3

You could do this:

Z = compass(eig(randn(5)));
colors = get(0,'DefaultAxesColorOrder')
for i=1:length(Z)
    set(Z(i),'color',colors(mod(i-1,length(colors))+1,:))
end    
Amro
  • 123,847
  • 25
  • 243
  • 454
cyborg
  • 9,989
  • 4
  • 38
  • 56