MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by IceQueen
on 26 Dec 2012

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!

*No products are associated with this question.*

Answer by Teja Muppirala
on 28 Dec 2012

Accepted answer

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'})

Answer by Walter Roberson
on 27 Dec 2012

You need to do a rotation (and possibly a translation as well) on your (x,y) data. Possibly the easiest way to do that would be to create the plot line in a hggroup and then do a hgtransform to do the rotation and translation.

IceQueen
on 27 Dec 2012

i will have a look on those two functions. thanks.

edit: it might help, but i have for every point already the value.. just turning the graph around leaves me with the problem on how i would align it to the right 'point' on the trajectory (which btw will not be color coded anymore)

Answer by Lalit Patil
on 27 Dec 2012

plot(data1) hold on plot(data2)

IceQueen
on 27 Dec 2012

thanks, but thats not the problem. I work a lot with hold on (i even have an image as background, but not for the purpose of the question). The thing is that every single point has a value and i need to put that at a distance away in an right angle at that point... maybe the easiest is to somehow find the tangential of the whole grapg in several points and then take that as new x-axis, but i really don't know how to do that.

## 0 Comments