Plotting two vectors for a period of time

3 views (last 30 days)
Hi,
I have a very simple question. I have two vectors x and y, which I plot against each other. I have another vector P which shows the evolution of the values of x against time. I want to extend the x vs y plot for the time given and highlight the values of P at each time. This should look just like the extension of my original curve, but with a line running across on it through time.
  1 Comment
Osama Maqbool
Osama Maqbool on 17 Aug 2016
As an example, x=[1.1 2.4 5 6 9.6] y=[0.2 0.3 0.5 0.67 0.9] this is the data for a characteristic curve of a device. Now I have the values x takes for 100 seconds and I want to show x,y and t on a 3d plot. And on every t, I want to point the value x has and the corresponding value of y.

Sign in to comment.

Accepted Answer

Robert
Robert on 17 Aug 2016
% From your question:
N = 100;
x = [1.1 2.4 5.0 6.00 9.6];
y = [0.2 0.3 0.5 0.67 0.9];
% A little more setup
time = (1:N)/4;
xy_index = randi(length(x),size(time)); % The active (x,y) index at each time
% To make line crawl across surface instead of through it, we need to do
% some interpolation. We will plot the original data with markers to
% hilight it and plot the interpolated data with lines.
% The index for x and y might move from any index to any other index. Here
% we use the diff to see how far it moved and fill in the spaces in
% between. So a jump from 1 to 4 is replaced with the indices [1,2,3,4]. If
% there is no jump (2 to 2 for example) we still need a spot for the second
% value, which is why we use max(1,abs(a)) below.
%
% Because each pair of indices will be replaces with an array of unknown
% length, we use arrayfun to package all the ragged arrays in a cell array,
% then concatenate them together in the second line.
xy_index_interpd = arrayfun(@(a)sign(a)*ones(1,max(1,abs(a))),diff(xy_index),'Uniform',false);
xy_index_interpd = [xy_index_interpd{:}];
xy_index_interpd = cumsum([xy_index(1),xy_index_interpd]);
% This is very much like the above lines, except that we are calculating how
% much we need to advance the time for each partial step across the
% surface. For this we use 1/number of partial steps so that the partial
% steps always add up to a whole step in the end.
t_index_interpd = arrayfun(@(a)ones(1,max(1,abs(a)))/max(1,abs(a)),diff(xy_index),'Uniform',false);
t_index_interpd = [t_index_interpd{:}];
t_index_interpd = cumsum([1,t_index_interpd]);
% Then we get our time values by interpolating time against the new
% indices.
t_interpd = interp1(1:N,time,t_index_interpd);
% And finally, we plot the results. A surf with x,y extended over time and
% a line to hilight the values of (x,y) over time.
surf(x,time,repmat(y,[length(time),1]),'EdgeColor','none') % y is expanded for surf
hold on
% The easy part
plot3(x(xy_index),time,y(xy_index),'ro')
% The harder part (for which we did all that interpolation above.
plot3(x(xy_index_interpd),t_interpd,y(xy_index_interpd)+max(abs(y))/1000,'r-') % ever so slightly above the surface for cleaner rendering
hold off
  2 Comments
Robert
Robert on 17 Aug 2016
In case you are wondering why we would do all that work, here is the same plot using only
plot3(x(xy_index),time,y(xy_index),'r-o')
as the trace across the surface.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!