MATLAB Answers

Graph Legend not picking up third plot marker

14 views (last 30 days)
Chloe Whitton
Chloe Whitton on 8 Apr 2021 at 19:52
Answered: Adam Danz on 8 Apr 2021 at 20:19
I've created a graph and tired to add in a legend to describe what each plot is however, MATLAB doesn't seem to be picking up the symbol for 'Toe off', it should be the red square. Does anyone know how to fix this issue? I've attached a screenshot of the code and the graph produced.

Answers (2)

Star Strider
Star Strider on 8 Apr 2021 at 20:16
It is likely not possible to determine what the problem is without the actual data and relevant code.
In their absence, try this:
hp{1} = plot(time, ankle_V);
hold on
hp{2} = plot(time(ankle_HS_V), zeros(size(ankle_HS_V)), 'g*');
hp{3} = plot(time(ankle_TO_V), zeros(size(ankle_TO_V)), 'rs');
hold off
legend([hp{:}],'Ankle Velocity', 'Heel Strike', 'Toe Off')
See if that does what you want.

Adam Danz
Adam Danz on 8 Apr 2021 at 20:19
There are multiple objects with the green asterisk and they are higher up in the uistack than the red squares. Since you're providing only 3 legend strings, the legend is choosing the first 3 items in the uistack which, apparently, are the blue line and the first two green astersks.
Instead use the DisplayName property to assign legend strings and then specify which object handles should appear in the legend
Follow this example.

Community Treasure Hunt

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

Start Hunting!