I have some given code and need to add a button that clears/deletes the plot poitns when pressed
2 次查看(过去 30 天)
显示 更早的评论
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?
回答(1 个)
Sanchari
2023-11-29
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!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!