how to plot points in a GUI

I have a Gui that I am trying to plot random points in but everytime I plot something it erases the button I have made in the GUi and the point doesnt show up on the graph portion. how do i fix this issue?

7 Comments

Attach your files (.mlapp file if using App Designer or .m and .fig files if using GUIDE) with the paperclip icon after you read this:
Learn App Designer here:
@Amanda: It appears you are creating your GUI programmatically (without using AppDesigner or GUIDE), using code as follows (copied from here):
f= figure;
ax= axes('Parent',f,'Position',[0.05, 0.25, 0.9, 0.7]);
hold on;
my_button = uicontrol('style','pushbutton');
set(my_button, 'units', 'normalized', 'position', [0.05,0.05,0.3,0.15])
set(my_button, 'string','Add Point')
my_second_button = uicontrol('style','pushbutton');
set(my_second_button, 'units', 'normalized', 'position', [0.35,0.05,0.3,0.15])
set(my_second_button, 'string', 'Clear')
my_third_button = uicontrol('style', 'pushbutton');
set(my_third_button, 'units', 'normalized','position', [0.65,0.05,0.3,0.15])
set(my_third_button, 'string', 'Randomize')
Can you share the code you're using to plot the random points?
And also please attach some screen shots. In particular, include a screen shot of how the figure window looks after the buttons are "erased".
Here is the script
here is the result
Not sure why the buttons appear to go away. They persist when I run the code here. (I've added a data marker in the plot call so that the single point shows up.)
f= figure;
ax= axes('Parent',f,'Position',[0.05, 0.25, 0.9, 0.7]);
x = 0.25;
y = 0.6;
plot(x,y,'.');
hold on;
my_button = uicontrol('style','pushbutton');
set(my_button, 'units', 'normalized', 'position', [0.05,0.05,0.3,0.15])
set(my_button, 'string','Add Point')
my_second_button = uicontrol('style','pushbutton');
set(my_second_button, 'units', 'normalized', 'position', [0.35,0.05,0.3,0.15])
set(my_second_button, 'string', 'Clear')
my_third_button = uicontrol('style', 'pushbutton');
set(my_third_button, 'units', 'normalized','position', [0.65,0.05,0.3,0.15])
set(my_third_button, 'string', 'Randomize')
Can you run the following line after running your script (or add it to the end of your script), and see if it returns 1 or 0?
ishandle(my_button)
Also, the fact that your axes still has default x- and y-limits ([0 1]) makes me think your script is plotting into another axes somehow and/or the buttons are being created in another figure somehow.
Why did you post your code as an image? Now anyone who wants to run your code will have to type it. Please replace the image with your code.
Also, when using GUIs, you should always use explicit handles when using functions like plot. That way you ensure that plots will appear where you expect them.

Sign in to comment.

Answers (0)

Tags

Asked:

on 10 Nov 2022

Commented:

Rik
on 11 Nov 2022

Community Treasure Hunt

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

Start Hunting!