Order of plot criteria

5 Ansichten (letzte 30 Tage)
Calum
Calum am 30 Mär. 2023
Kommentiert: Calum am 30 Mär. 2023
Hi everyone, hope you are well.
I am currently experiencing an issue with the use of plotting whilst using a dynamic legend within a loop.
I have a series of 'if' statements with certain plotting criteria inside of each of them - this is because i would like a plot to be formed AND a legend addition to be made based ONLY if said condition is met, and to ignore this otherwise. My problem is basically that i am struggling with plot syntax haha. A sample of the code looks like this(note this is all inside a larger global for loop):
legend('-DynamicLegend')
hold all;
if norm(AZ_VT)~=norm(y)
plot(Az_IT_time,Az_IT,'r.','MarkerSize',3)
end
There are 3 other subsequent if statements like this.
My problem is that, now i have established the Dynamic Legend - I'd like to add this in if the condition is met, and ALSO display the number of the global iteration WITHIN the name of each legend entry.
So i've tried something like this for the plotting line above but keep getting errors:
plot(AZ_IT_time,AZ_IT,'r.','MarkerSize',3,DisplayName,'Item Number %d', num2str(Item))
where 'Item is simply a global loop counter.
I'm wondering if anyone that knows proper plotting syntax and the use of dynamic legends/including changing items in legends can help.
Thanks a lot.
C.

Akzeptierte Antwort

Jack
Jack am 30 Mär. 2023
Hi C,
I'd be happy to help you with your code. From what I understand, you want to create a dynamic legend that includes the number of the global iteration within the name of each legend entry, and you want to add the legend only if the condition in each 'if' statement is met.
To accomplish this, you can use the 'DisplayName' property of the plot function to set the name of the plot in the legend. Here's an example of how you can modify your code to achieve the desired result:
% Initialize dynamic legend
legend('-DynamicLegend');
for Item = 1:numItems % numItems is the total number of items in the loop
% your code for each item goes here...
% check if condition is met
if norm(AZ_VT) ~= norm(y)
% plot the data and set the DisplayName property
h = plot(AZ_IT_time, AZ_IT, 'r.', 'MarkerSize', 3, 'DisplayName', sprintf('Item Number %d', Item));
% add the plot to the legend
addlistener(h, 'Annotation', @(~,~)set(get(gca,'Legend'),'String',unique(get([get(gca,'Children')],'DisplayName'))));
end
% your code for each item goes here...
end
% show the legend if there are any plots with DisplayName property set
if any(strcmp(get(gca,'Type'),'axes')) && any(strcmp(get(get(gca,'Legend'),'String'),'DisplayName'))
legend('show');
end
Here, I've added a for loop to iterate over each item in your larger loop. Within the loop, you can use the sprintf function to create a legend entry with the current item number, and then set the DisplayName property of the plot to this legend entry.
To update the dynamic legend, you can add a listener to the plot using the addlistener function. This listener will execute a function that updates the legend every time the plot is changed.
Finally, I've added some code at the end to show the legend only if there are any plots with DisplayName property set.
I hope this helps! Let me know if you have any further questions.
  2 Kommentare
Calum
Calum am 30 Mär. 2023
Hi Jack - thank you so much for your assistance I really appreciate it.
I've updated my script with the above, though I'm having a slight issue with the listener. I am shown an error which says:
'Event 'Annotation' is not defined for class 'matlab.graphics.chart.primitive.Line'
I am using the 2020b version of the software at the moment. Do you know any other way around this?
Calum
Calum am 30 Mär. 2023
[Update] Hi Jack - turns out this works perfectly fine without the listener! The dynamic legend function seems to recognise each individual plot occurrence and apend this to a legend automatically so long as the 'DisplayName' Property is present as you've shown me.
Thanks so much youve been a massive help!
C.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by