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:
- 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.
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.