2 different markers in loop, show both when applicable
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Akzeptierte Antwort
dpb
am 5 Sep. 2018
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 Kommentare
dpb
am 6 Sep. 2018
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!