Set uicontrol position relative to a subplot

20 次查看(过去 30 天)
Is it possible to set the position of a GUI control element (e.g. a slider) relative to a subplot's position rather than relative to the whole figure?
The code I'm trying to write can have a variable number of subplots and it would easiest if I could set the uicontrol 'Position' relative to the subplot. For example, I might have two or three subplots and I want a slider under each of them.
Thanks!

采纳的回答

Walter Roberson
Walter Roberson 2015-5-6
No.
However, you can create a uipanel and the panel can contain axes and uicontrol such as sliders. uicontrol() 'Position' properties are relative to the container, not to the figure. axes cannot contain uicontrol, but uipanel can.
For example:
fignum = figure('Units', 'normal', 'Position', [0.1 0.1 .8 .8]); %not quite full screen
subgroup1 = uipanel('Parent', fignum, 'Units', 'normal', 'Position', [0 2/3 1 1/3]) %top third
subgroup1_plotbox = uipanel('Parent', subgroup1, 'Units', 'normal', 'Position', [0 .1 1 .9]) %plot in top 9/10 of the group
subgroup1_controls = uipanel('Parent', subgroup1, 'Units', 'normal', 'Position', [0 0 1 .1]); %control area in bottom 1/10 of the group
subgroup1_axes = axes('Parent', subgroup1_plotbox);
plot(1:50, rand(1,50), 'Parent', subgroup1_axes); %throw up some content
subgroup1_slider = uicontrol('style', 'slider', 'Parent', sugroup1_controls, ..... );
You do not actually need to create the two sub-panels for the above purpose, as you could put both the axes and the slider into 'Parent', subgroup1, making appropriate adjustments to the Position properties for those. The above example is for illustrative purposes, showing how you can create groups of objects in positional relationship to other groups of objects. You could resize the plot area and group of controls by adjusting the Position of the appropriate uipanel relative to the containing object. For simpler cases, that isn't worth the effort.
Each uipanel can act much like a figure (with some differences like not having individual toolbars), including uipanel being able to contain other uipanel.

更多回答(2 个)

Joseph Cheng
Joseph Cheng 2015-5-6
well your explanation of the problem is exactly how you'd do it. you would first get the position of the subplot then perform the offset.
this isn't pretty but for a 2 min example it'll show you what you can accomplish
clf;
for ind =1:3
hsub(ind) =subplot(3,1,ind),plot(randi(10,1,10));
subpos = get(hsub(ind),'position');
hslide(ind) = uicontrol('style','slider')
set(hslide(ind),'units',get(hsub(ind),'units'))
slideoff = [-.05 -.05 0];
slidepos = get(hslide(ind),'position')
slidepos = [subpos(1:3)+slideoff slidepos(end)]
set(hslide(ind),'position',slidepos);
end

Mary
Mary 2015-5-7
Thanks to both of you for your helpful responses. I got it working well by grouping elements using subpanels. The trick was to not use subplots at all, but just generate equivalently-sized subpanels and work with those instead. Thanks for the rapid and very helpful responses!

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by