I have some given code and need to add a button that clears/deletes the plot poitns when pressed

2 visualizaciones (últimos 30 días)
I have been given some code and that automatically plots some points on a graph. I have then added a button but don't know to make a callback to the button. So when the button is pressed it will delete the points on the graph. Will I need to feed in the graph? Do I make an additional function or can I do it within the button command?
  3 comentarios

Iniciar sesión para comentar.

Respuestas (1)

Sanchari
Sanchari el 29 de Nov. de 2023
Hello Katherine,
I understand that you want to add a button to an existing code (of making a plot) and create a callback function for it to enable it to clear or delete data points from the plot.
To create a callback for a button in MATLAB programmatically that deletes points from a graph when pressed, you can follow the example code given below:
function plotApp
fig = uifigure;
g = uigridlayout(fig,[2 3]);
g.RowHeight = {'1x','fit'};
g.ColumnWidth = {'1x','fit','1x'};
ax = uiaxes(g);
ax.Layout.Row = 1;
ax.Layout.Column = [1 3];
b = uibutton(g, "Text","Plot Data", "ButtonPushedFcn", @(src,event) plotButtonPushed(ax));
c = uibutton(g, "Text","Delete Plot Data","ButtonPushedFcn",@(src,event) deletePlotButtonPushed(ax));
b.Layout.Row = 2;
b.Layout.Column = 1;
c.Layout.Row = 2;
c.Layout.Column = 3;
end
function plotButtonPushed(ax)
x = linspace(0,2*pi,100);
y = sin(x);
plot(ax,x,y)
end
function deletePlotButtonPushed(ax)
cla(ax)
end
Here the “deletePlotButtonPushed(ax)” is the button callback function that deletes all the plotted data points. The output for the above code should look something like this after pressing “Plot Data” button.
Next, press the “Delete Plot Data” button and the output should look something like this.
Alternatively, you can run the following command in the MATLAB command window to open the above MathWorks example in MATLAB editor.
>> openExample('matlab/UibuttonCodeResponseToClickExample')
Also consider the following MATLAB File Exchange resources if it suits better to your need for deleting the points from plot:
  1. MATLAB File Exchange link for Graphical Data Selection Tools: https://www.mathworks.com/matlabcentral/fileexchange/13857-graphical-data-selection-tool?s_tid=srchtitle_support_results_2_button%20to%20delete%20plot%20data
  2. MATLAB File Exchange link for “sel_and_del function to Interactively select and delete points from a 2D plot with the mouse: https://www.mathworks.com/matlabcentral/fileexchange/25153-sel_and_del-interactively-select-and-delete-points-from-a-2d-plot-with-the-mouse?s_tid=srchtitle_support_results_4_button%20to%20delete%20plot%20data
  3. MATLAB File Exchange link for “addplot” function: https://www.mathworks.com/matlabcentral/fileexchange/795-addplot-m?s_tid=srchtitle_support_results_3_button%20to%20delete%20plot%20data
Hope this information is helpful to you!

Categorías

Más información sobre Migrate GUIDE Apps en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by