2 different markers in loop, show both when applicable
1 次查看(过去 30 天)
显示 更早的评论
I have a loop processing the VEI index of volcanoes for various years.
Say in some years, VEI4lat is [0]. The var exists, but the values are 0. The map won't plot anything and the legend disappears. The volcsymbol2 doesn't show for all my plots now, and the legend also doesn't show the volcsymbol2. My map plots and legend only show volcsymbol...
% marker for VEI <= 3
volcsymbol = plotm(VEI3lat,VEI3long,'^','markersize',8,'markerfacecolor','r', ...
'markeredgecolor','k','linewidth',0.5);
if isequal(VEI4lat,[0]) == 0,
elseif isequal(VEI4lat,[0]) == 1
% Volcano marker for VEI >= 4
volcsymbol2 = plotm(VEI4lat,VEI4long,'^','markersize',8,'markerfacecolor','k', ...
'markeredgecolor','k','linewidth',0.5);
end
% Creating legend on map
if isequal(VEI4lat,[0]) == 1
legend([volcsymbol,volcsymbol2],{'VEI <= 3','VEI >= 4'},'Location','Southeast')
elseif isequal(VEI4lat,[0]) == 0,
legend([volcsymbol],{'VEI <= 3'},'Location','Southeast')
end
3 个评论
采纳的回答
dpb
2018-9-5
if isequal(VEI4lat,[0]) == 0,
is equivalent to
if VEI4lat~=0
and the block is empty so nothing ever happens for any case except zero.
It looks like it should plot just fine for the zero case; are the plot limits such that they include zero; that would be a likely reason don't see data on a plot that think should be there.
The above could be written much succinctly as
if VEI4lat==0
% Volcano marker for VEI >= 4
volcsymbol2 = plotm(VEI4lat,VEI4long,'^','markersize',8,'markerfacecolor','k', ...
'markeredgecolor','k','linewidth',0.5);
legend([volcsymbol,volcsymbol2],{'VEI <= 3','VEI >= 4'},'Location','Southeast')
else
legend([volcsymbol],{'VEI <= 3'},'Location','Southeast')
end
since there's nothing in the ~= case, it's superfluous for plotting but the subsequent if block for legend may as well be folded in.
BUT, be aware that there can only be one legend and so whichever is the one that is last called will be what is left, in general one should save the handles to the line objects from plot and assign labels to the specific handles desired for the legend.
2 个评论
dpb
2018-9-6
Ah! We can't tell from your original posting what what VEI4lat, etc., are...if they are vectors then use
if all(VEI4lat)==0
I had presumed they were going to be single values at that point.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Legend 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!