I have some given code and need to add a button that clears/deletes the plot poitns when pressed
Mostra commenti meno recenti
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 Commenti
Jorge Párraga Ortega
il 21 Nov 2023
maybe you could try to make a function that close teh graph and replote it without that point that you mention
Mario Malic
il 21 Nov 2023
Cris LaPierre
il 21 Nov 2023
Risposte (1)
Sanchari
il 29 Nov 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.

You can also refer this example from MathWorks Documentation: https://www.mathworks.com/help/matlab/ref/uibutton.html#mw_257036be-77b2-4e83-9446-5e15e39fc6fd.
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:
- 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
- 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
- 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!
Categorie
Scopri di più su Develop Apps Programmatically in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!