# How to plot a second graph instead of color-coding

5 views (last 30 days)

Show older comments

Hello everyone,

i just started with my master thesis and i already am in trouble with my capability/understanding of matlab.

The thing is, i have a trajectory on a surface of a planet/moon whatever (a .mat with the time, and the coordinates. Then i have some .mat with time and the measurement at that time.

I am able to plot this as a color coded trajectory (using the measurement and the coordinates) in scatter(). This works awesomely nice.

However my problem is that i need something more sophisticated. I now need to take the trajectory and instead of color-coding it, i am supposed to add the graph (value) of the measurement (which is given for each point) to the trajectory (which is not always a straight line). I will added a little sketch to explain what i want. The red arrow shows what i want to add to my plot and the green shows what i have.

I would be really thankful for any kind of suggestion or help!

##### 0 Comments

### Accepted Answer

Teja Muppirala
on 28 Dec 2012

Assumung your XY data are smooth, you can find the normal vector at each point, and then offset in that direction, like the code below. If your position data is not smooth, then you'll need to smooth it before you differentiate and get the normals or otherwise you'll get a bunch of noise.

%%%%%%%%%%Step 0. Just making some random data... %%%%%%%%%%

% Position

rng(16);

x = interpft(3*rand(1,4),1000);

y = interpft(3*rand(1,4),1000);

x = x(1:200);

y = y(1:200);

% Measurement

m = 0.1*interp1(1:20,rand(1,20),linspace(1,20,200),'pchip');

% Plot it

subplot(211);

plot(x,y,'r','linewidth',2);

hold on;

plot(x(1),y(1),'rx','markersize',10);

title('XY position');

axis equal;

grid on;

subplot(212);

plot(m,'k','linewidth',2);

title('Measurement');

%%%%%%%%%%Step 1. Find the normal vectors %%%%%%%%%%

% First find the velocity using finite differences

dx = conv(x,[-0.5 0 0.5],'valid'); % Same as (x(3:end) - x(1:end-2)) / 2

dx = [dx(1) dx dx(end)]; % Take care of the endpoints

dy = conv(y,[-0.5 0 0.5],'valid');

dy = [dy(1) dy dy(end)];

% Normalize it to unit length

V = [dx; dy];

Vmag = sqrt(sum(V.^2)); % Magnitude of velocity

V1 = bsxfun(@rdivide, V, Vmag); % Velocity vector normalized to 1

% Swap the components to get the normals

N1 = [V1(2,:); -V1(1,:)];

subplot(211)

quiver(x,y,N1(1,:),N1(2,:),0.5);

%%%%%%%%%%Step 2. Shift the measurement to the new axes %%%%%%%%%%

m_shifted = [x;y] + bsxfun(@times,m,N1);

plot(m_shifted(1,:),m_shifted(2,:),'k','linewidth',2);

hold off;

legend({'XY Path', 'Start pt.', 'Normals', 'Measured'})

### More Answers (2)

Walter Roberson
on 27 Dec 2012

### See Also

### Categories

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!