Legend when plot is inside a for loop

1 view (last 30 days)
I could not get the legend to be right for this plot (which is inside a for loop). Any ideas what might have gone wrong?
clc;
close all;
clear all;
n4=4;
alpha=[0.01,0.04,0.07,0.1];
psi_grid = [1,2.555555555555555,4.111111111111111,5.666666666666667,...
7.222222222222222,8.777777777777779,10.333333333333334,...
11.888888888888890,13.444444444444445,15];
c = [0.0447669820741666,0.0447661557840448,0.0447591680513355,...
0.0447599101173151;0.170310830973294,0.170309215966225,...
0.170307375023580,0.170216706811469;0.324091538659768,...
0.324090724159454,0.324068002022192,0.199586696187332;...
0.492551604649453,0.492544368227092,0.418894624542177,...
0.214192778682781;0.669337278714228,0.669324577104975,...
0.452742963965304,0.223854115287130;0.850738640840670,...
0.850703370114468,0.478623618230665,0.230735577427633;...
1.03431601322415,1.03397088453076,0.499136989845354,...
0.235892409514585;1.21834098937911,1.11782404435437,...
0.515833589468893,0.239903478911979;1.40154955099879,...
1.17392236326210,0.529677715141711,0.243113765553547;...
1.58294248656178,1.22364826729296,0.541376923264488,0.245741990469332];
xlb=0;
xub=16;
xubtick=16;
figure('name','Fig.1','number','off');
set(gcf,'units','inches','renderer', 'painters');
pos = get(gcf,'pos');
set(gcf,'Units','inches',...
'Position',[pos(1) pos(2) 2.9 2]);
xlabel('parameter')
ylabel('sensitivity')
set(gca,...
'Units','normalized',...
'FontUnits','points',...
'FontWeight','normal',...
'FontSize',9,...
'FontName','Times',...
'XLim',[xlb xub],...
'XTick',xlb:2:xubtick,...
'YTick',0:.4:1.6,...
'YLim',[0 1.6],...
'Box','on')
set(gca, 'Position', get(gca, 'OuterPosition') - ...
get(gca, 'TightInset') * [-1 0 1 0; 0 -1 0 1; 0 0 1 0; 0 0 0 1]);
for k1 = 1:n4
plot(psi_grid,c(:,k1));
hold all;
ar= sprintf('{\\it\\alpha =}%s',rats(alpha(k1),6));
drawnow;
end
gridxy(get(gca,'xtick'),get(gca,'ytick'),'color',[.75 .75 .75],'LineWidth',.5,'Linestyle',':');
hold off;
legend(ar,'Location','best');

Accepted Answer

Walter Roberson
Walter Roberson on 22 Sep 2015
ar{k1} = sprintf('{\\it\\alpha =}%s',rats(alpha(k1),6));
  1 Comment
John
John on 22 Sep 2015
This worked. There was still a problem in my code above so that one entry in the legend was referring to the grid. Putting the gridxy as the very last command did the trick, even though I don't understand why:
...
hold off;
legend(ar,'Location','best');
gridxy(get(gca,'xtick'),get(gca,'ytick'),'color',[.75 .75 .75],'LineWidth',.5,'Linestyle',':');

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!