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

조회 수: 2 (최근 30일)
Jessica Webb
Jessica Webb 2017년 9월 20일
답변: Jessica Webb 2017년 9월 21일
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

답변 (2개)

Simon Henin
Simon Henin 2017년 9월 21일
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
Walter Roberson
Walter Roberson 2017년 9월 21일
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'})

댓글을 달려면 로그인하십시오.


Jessica Webb
Jessica Webb 2017년 9월 21일
Thanks for this, will take a look. Really appreciate it!

카테고리

Help CenterFile Exchange에서 Frequently-used Algorithms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by