Filter löschen
Filter löschen

Adding a linear line of best fit to subplots

4 Ansichten (letzte 30 Tage)
Jonathan O'Neill
Jonathan O'Neill am 30 Jan. 2015
Kommentiert: Jonathan O'Neill am 30 Jan. 2015
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')

Akzeptierte Antwort

Image Analyst
Image Analyst am 30 Jan. 2015
See my attached polyfit demo. I think it should answer your questions.
  1 Kommentar
Jonathan O'Neill
Jonathan O'Neill am 30 Jan. 2015
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')

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by