How to randomize points using a GUI

1 回表示 (過去 30 日間)
Amanda
Amanda 2022 年 11 月 30 日
コメント済み: David Hill 2022 年 12 月 1 日
I have created a GUI that has a push button that you can add points to the graph and a button to randomize the added point. I had to plot a singular point and then later created a function that adds points. when I click the randomize button it only randomizes the first point I created but I need it to randomize for all of the points that get added. I'm not sure how to do so but here is what I have created so far:
f= figure;
ax= axes('Parent',f,'Position',[0.05, 0.25, 0.9, 0.7]);
xlim = ([0,1]);
ylim = ([0,1]);
x = 0.25;
y = 0.55;
point_plot_handle = plot(x,y,'d','MarkerFaceColor','m','MarkerSize',75);
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')
set(my_button, 'callback', @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')
set(my_second_button, 'callback', @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')
set(my_third_button, 'callback', {@randomize,point_plot_handle});
function randomize(sender, eventdata, h)
new_point = randn(2,1);
set(h,'XData', new_point(1), 'YData', new_point(2));
end
function clear(sender, eventdata)
empty = cla;
end
function add_point(~,~)
l='od+.';
plot(rand,rand,l(randi(4)),'MarkerFaceColor',rand(1,3),'MarkerSize',35);
end

採用された回答

David Hill
David Hill 2022 年 11 月 30 日
編集済み: David Hill 2022 年 11 月 30 日
Please accept my previous answer if it was acceptable to you.
GUI
function GUI()
f= figure;
axes('Parent',f,'Position',[0.05, 0.25, 0.9, 0.7]);
x = 0.25;
y = 0.55;
h = plot(x,y,'d','MarkerFaceColor','m','MarkerSize',35);
ax=gca;
ax.XLim = [0,1];
ax.YLim = [0,1];
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')
set(my_button, 'callback',@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')
set(my_second_button, 'callback', @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')
set(my_third_button, 'callback', @randomize);
function randomize(~,~)
for k=1:length(h)
h(k).XData=rand;
h(k).YData=rand;
end
end
function clear(~,~)
cla;
h=[];
end
function add_point(~,~)
l='od+.';
h=[h,plot(rand,rand,l(randi(4)),'MarkerFaceColor',rand(1,3),'MarkerSize',35)];
end
end
  3 件のコメント
Amanda
Amanda 2022 年 12 月 1 日
my professor talked about storing it in an array by storing all the handles of my plots in it then addind the new plot handle to the end of the existing one. his example was plot_handle_array= [plot_hanlde_array new_plot_handle]. I dont understand this or how to do it
David Hill
David Hill 2022 年 12 月 1 日
The above works. I am storing all the lines in variable h. Just run the function GUI.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by