필터 지우기
필터 지우기

Adding a linear line of best fit to subplots

조회 수: 4 (최근 30일)
Jonathan O'Neill
Jonathan O'Neill 2015년 1월 30일
댓글: Jonathan O'Neill 2015년 1월 30일
I have been looking up matlab help on this but I cant seem to get my head around it. For example when I look at my code I just have:
plot (SigVal)
So when I try and plot:
polyval(x, y 1)
I have nothing to put between these brackets except "SigVal". Can someone talk me through how I go about adding a line of best fit to this subplot?
thanks
%-------------------------------------- SIG VAL SUBPLOT -------------------
figure('name','resultimage');
Len=1400;
Wid=600;
aa=25;
bb=200;
set(gcf,'position',[aa bb Len Wid]);
subplot(2,3,1)
xmin=1;
xmax=100;
plot(SigVal)
h=plot(SigVal);
% label max and min values of a 'fn1' on the plot
xValue = get(h,'XData');
yValue = get(h,'YData');
imin = find(min(yValue)==yValue);% find the index for the min and max
imax = find(max(yValue)==yValue);% values of yValue
(yValue(imax));
(yValue(imin));
% label the max. and min. values on the plot
ymax=yValue(imax)+3;
ymin=yValue(imin)-3;
y1=ymax-1;
y2=y1;
y3=ymin+1;
maxstr=['max.=',num2str(yValue(imax))];
minstr=['min.=',num2str(yValue(imin))];
text(10, y1, maxstr);
text(50, y2, minstr);
if Signalstabilitytestfail == 1;
Test_resultstr = 'Test FAILED';
text(30, y3, Test_resultstr)
sigvalteststr='fail';
else
Test_resultstr = 'Test passed';
text(30, y3, Test_resultstr)
sigvalteststr='pass';
%warndlg('Signal Stability failed','!! Warning !!')
end
axis([xmin xmax ymin ymax])
title('SigVal')

채택된 답변

Image Analyst
Image Analyst 2015년 1월 30일
See my attached polyfit demo. I think it should answer your questions.
  댓글 수: 1
Jonathan O'Neill
Jonathan O'Neill 2015년 1월 30일
Thank you, I found some code you provided to answer another question and followed that...
http://www.mathworks.com/matlabcentral/answers/89335-how-do-i-make-a-best-fit-line-along-with-getting-r-2-on-matlab
I replaced my code and it worked, although it gave me a graph with dots, in order to have the dots connected I did this...
Changing
"h=plot(x, y, 'b*', 'LineWidth', 1, 'MarkerSize', 1";
to
"h=SigVal" gave me connected lines and that seems to be all it affected.
%-------------------------------------- SIG VAL SUBPLOT -------------------
figure('name','resultimage');
Len=1400;
Wid=600;
aa=25;
bb=200;
set(gcf,'position',[aa bb Len Wid]);
subplot(2,3,1)
xmin=1;
xmax=100;
x=1:100
y = SigVal
plot(x, y, 'b*', 'LineWidth', 1, 'MarkerSize', 1);
coeffs = polyfit(x, y, 1);
% Get fitted values
fittedX = linspace(min(x), max(x), 200);
fittedY = polyval(coeffs, fittedX);
% Plot the fitted line
hold on;
plot(fittedX, fittedY, 'r-', 'LineWidth', 1);
********************************************************
%h=plot(x, y, 'b*', 'LineWidth', 1, 'MarkerSize', 1);
h=plot(SigVal);
********************************************************
% label max and min values of a 'fn1' on the plot
xValue = get(h,'XData');
yValue = get(h,'YData');
imin = find(min(yValue)==yValue);% find the index for the min and max
imax = find(max(yValue)==yValue);% values of yValue
(yValue(imax));
(yValue(imin));
% label the max. and min. values on the plot
ymax=yValue(imax)+1.5;
ymin=yValue(imin)-1.5;
y1=ymax-1;
y2=y1;
y3=ymin+1;
maxstr=['max.=',num2str(yValue(imax))];
minstr=['min.=',num2str(yValue(imin))];
text(10, y1, maxstr);
text(50, y2, minstr);
if Signalstabilitytestfail == 1;
Test_resultstr = 'Test FAILED';
text(30, y3, Test_resultstr)
sigvalteststr='fail';
else
Test_resultstr = 'Test passed';
text(30, y3, Test_resultstr)
sigvalteststr='pass';
end
axis([xmin xmax ymin ymax])
title('SigVal')

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by