How can I display data in order in the legend?

2 views (last 30 days)
I want to display the legend as; black dot as actual data, green dot as data fit and red line starting from PolyCurve4/mum and so on. Secondly, If I want just to diplay PolyCurves without showing black dots and green dots in the plot, what modification would I need?
clc
clear all
load EC.mat
for n=4:1:6;
filename = sprintf('data_%d.mat',n);
load(filename)
E(n-3,:)=EC(1,:);
d(n-3,:)=dep(9,:);
end
hold on ; % MOVE THIS OUTSIDE THE LOOP
col=['r' 'y' 'b' 'g']; % MOVE THIS OUTSIDE THE LOOP
[m,~]=size(d);
lineHandles = gobjects(1,m); % ADD THIS
for i=1:m
PP(i,:)=plot(d(i,:),E(i,:),'ok','LineWidth',2)% original data
P1(i,:) = polyfit(d(i,:),E(i,:),2);%%2 indicates the quadratic
% x_values = min(in_(1,:)): 0.001:max(x_axis(1,:));
Y= polyval(P1(i,:),d(i,:));
h1 = plot(d(i,:),Y,'*g','LineWidth',1) % fitted
Y1 = polyval(P1(i,:),d(i,:));
R1 = corrcoef(Y1,d(i,:));
%%lsline
ss = min(d(i,:)) : 0.001: max(d(i,:));
Y2 = polyval(P1(i,:),ss);
lineHandles(i) = plot(ss,Y2,'color',col(i),'LineWidth',1); % STORE HANDLE
end
% MOVE THIS OUTSIDE THE LOOP
title('Polynomial fitting','FontSize',16,'FontWeight','normal')
xlabel('Depolarisation ratio \delta_{in}','FontSize',12,'FontWeight','normal');
ylabel('Extinction coefficient \alpha{(m^-^1)}','FontSize',12,'FontWeight','normal');
ylim([0.005 0.03])
% SUPPLY HANDLES, DEFINE EACH POLY CURVE LINE
legend([PP(i,:), h1, lineHandles],...
'Actual data','Data fit',compose('PolyCurve %d\mum',(1:m+2)))

Answers (1)

Walter Roberson
Walter Roberson on 1 Feb 2021
With:
legend([PP(end,:), h1, lineHandles],...
["Actual data","Data fit",compose('PolyCurve %d\mum',(1:m+2))])
Without:
legend(lineHandles, compose('PolyCurve %d\mum',(1:m+2)))

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!