Set uicontrol position relative to a subplot

19 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Walter Roberson
Walter Roberson il 6 Mag 2015
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.

Più risposte (2)

Joseph Cheng
Joseph Cheng il 6 Mag 2015
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 il 7 Mag 2015
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!

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by