Hello, I want to plot two variables (with different number of phases)...

7 views (last 30 days)
Jessica Webb
Jessica Webb on 20 Sep 2017
Answered: Jessica Webb on 21 Sep 2017
Hello, I have two columns of data (one volume with 13 points, and the other pressure with 60 points).
I ultimately want to plot them against each other (they both start at 0 and finish at 400ms) but also want to know the exact volume and pressure for any time point between 0 and 400ms.
I am a matlab novice and so any help would be wonderful Thank you

Answers (2)

Simon Henin
Simon Henin on 21 Sep 2017
To plot your data against one another you could use plotyy
% make up some data
volume = [1:13]'+wgn(13,1, 10); % add some wgn noise to the simulated data
pressure = [1:60]'+wgn(60,1, 10);
% create time vectors, assuming each variable is linearly spaced (this may
% not be the case, but just an assumption)
t1 = linspace(0,400, 13)';
t2 = linspace(0,400, 60)';
% plot them against each other
ax = plotyy(t1, volume,t2, pressure); hold on;
ylabel(ax(1), 'Volume');
ylabel(ax(2), 'Pressure');
legend({'Volume', 'Pressure'});
Without knowing more information about the time vs. amplitude relationship of your data, it is hard to give you a direct answer as to how to interpolate your results to any given time point. However, if your data have a linear relationship (e.g. volume increases linearly as a function of time), then you could do a simple regression to get a best fit to your data, and interpolate from the result:
% If your variables have a linear relationship, them find a regressor that
% fits the data. Otherwise, need more info about the x-y relationship of
% your data to fit a proper curve (look at curve fitting toolbox for more info)
% create a vector of all times of interest (e.g. 0-400ms, in 1ms steps)
t3 = linspace(0,400, 401);
% find regression line that fits the data
X = [ones(length(volume),1) t1];
b = X\volume;
y = b*t3;
figure;
plot(t1, volume, 'o'); hold on;
plot(t3,y(2,:)); % plot the slope+intercept
plot(t3(100), y(2,100), '^', 'markersize', 18);
legend({'Volume', 'Regression Fit to Data', 'Data fit at t=100ms'});
  1 Comment
Walter Roberson
Walter Roberson on 21 Sep 2017
Instead of regression, you can use interp1:
starttime = 1;
endtime = 400;
timebase_v = linspace(starttime, endtime, length(volume));
timebase_p = linspace(starttime, endtime, length(pressure));
common_timebase = starttime : endtime;
interp_v = interp1(timebase_v, volume, common_timebase);
interp_p = interp1(timebase_p, pressure, common_timebase);
plot(common_timebase, interp_v, 'r', common_timebase, interp_p, 'b');
legend({'volume', 'pressure'})

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!