- Initialize the legend handles and labels arrays outside of your callback function. You should do this in the App Designer's startupFcn, so they persist between callback invocations.
- In your callback function, update the legend with all the handles and labels for all the data sets currently plotted.
Plot Legend and "hold on" issue
29 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hi,
I am having some issues with the plot legend in App Desinger.
I want to be able to plot a number of data sets on a single probability plot (this works with the code below), however, when I change data set variables and plot again, the data displays on the plot but the legend is overwritten with only the new data set legend.
For example, the code below goes in the plotbuttonpushed callback, so when I select a few checkboxes and press the "Plot" button, the data and the correct legend is plot as it should be. However, when I change some varibales in the data set, I select more checkboxes to plot more data and press plot again, the data is overlayed on to the plot correctly, but the legend is overwritten with the new data.
Is there anyway I can solve this?
% Filter data based on the checkboxes
plot_data = table2array(app.split_table_data);
selected_data = plot_data(:, app.UITable.Data{:, 2});
% Plotting
line_handles = probplot(app.UIAxes, selected_data, "noref");
set(line_handles, 'LineWidth', 1, 'Color', app.plot_colours(random_number,:));
xlabel(app.UIAxes, xAxisLabel);
title(app.UIAxes, 'Probability Plot');
% Generate new legend labels for the current data
if strcmpi(app.Technology_Drop_Down.Value, '40nm')
new_legend_labels = strcat(app.device_names(app.UITable.Data{:, 2}), " Temp: ", app.Temperature_Drop_Down.Value);
else
new_legend_labels = app.device_names(app.UITable.Data{:, 2});
end
if ~isfield(app, 'master_legend_handles')
app.master_legend_handles = [];
app.master_legend_labels = {};
end
% Append the new handles and labels to the master lists
app.master_legend_handles = [app.master_legend_handles; line_handles];
app.master_legend_labels = [app.master_legend_labels, new_legend_labels];
% Display the legend
legend(app.UIAxes, app.master_legend_handles, app.master_legend_labels, 'Location', 'Best');
hold(app.UIAxes, "on");
app.UITable.ColumnEditable = [false, false];
app.nSigma_Sigma_Edit_Field.Value = 0;
0 Kommentare
Antworten (1)
Yash
am 31 Aug. 2023
It seems like you're trying to update a legend in an App Designer GUI in MATLAB when you add new data sets to your probability plot. The issue you're facing is that when you plot new data, the legend is overwritten with the legend for the new data set. To solve this issue and maintain a cumulative legend, you can make the following modifications to your code:
Here's how you can modify your code:
In the App Designer, go to the startupFcn to initialize the legend handles and labels:
function startupFcn(app)
% Initialize the legend handles and labels
app.master_legend_handles = [];
app.master_legend_labels = {};
end
Now, in your callback function:
function PlotButtonPushed(app, event)
% Filter data based on the checkboxes
plot_data = table2array(app.split_table_data);
selected_data = plot_data(:, app.UITable.Data{:, 2});
% Plotting
line_handles = probplot(app.UIAxes, selected_data, "noref");
set(line_handles, 'LineWidth', 1, 'Color', app.plot_colours(random_number,:));
xlabel(app.UIAxes, xAxisLabel);
title(app.UIAxes, 'Probability Plot');
% Generate new legend labels for the current data
if strcmpi(app.Technology_Drop_Down.Value, '40nm')
new_legend_labels = strcat(app.device_names(app.UITable.Data{:, 2}), " Temp: ", app.Temperature_Drop_Down.Value);
else
new_legend_labels = app.device_names(app.UITable.Data{:, 2});
end
% Append the new handles and labels to the master lists
app.master_legend_handles = [app.master_legend_handles; line_handles];
app.master_legend_labels = [app.master_legend_labels, new_legend_labels];
% Display the legend
legend(app.UIAxes, app.master_legend_handles, app.master_legend_labels, 'Location', 'Best');
hold(app.UIAxes, "on");
app.UITable.ColumnEditable = [false, false];
app.nSigma_Sigma_Edit_Field.Value = 0;
end
By initializing and updating the app.master_legend_handles and app.master_legend_labels outside of your callback, the legend will accumulate all the data sets you've plotted, and it won't be overwritten when you plot new data.
I hope this helps.
0 Kommentare
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!