Order of plot criteria
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Akzeptierte Antwort
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.
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Legend finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!