In MATLAB GUI, how can I display a set of elements such as edit text and slider only if I push the radiobutton?

4 views (last 30 days)
To save space in some cases, I want to make the set of elements under the radiobutton to appear when I click it, and disappear when I click another radiobutton, so that only corresponding edit texts should be displayed when I push the radiobutton. Also when they disappear, the space they cover should disappear too. How can I do this?
  5 Comments
Ufuk Keskin
Ufuk Keskin on 18 Mar 2020
The code works like this:
But I want it to work like this:
So it basically taking the area covered by edit text along with the edit text
Geoff Hayes
Geoff Hayes on 19 Mar 2020
I still don't quite follow. Do you mean the space between the radio button and the edit text controls? Isn't that something you can control when you build the GUI? Please attach your code and fig file if you want.

Sign in to comment.

Accepted Answer

Rik
Rik on 19 Mar 2020
I suspect this is close to what you mean. I made it a programmatic GUI, instead of using GUIDE, as this provides more options for reusing code (as you can clearly see).
f=figure(1);clf(f)
h=struct;
h.radio=uicontrol('Parent',f,'style','radio',...
'Units','Normalized','Position',[0.2 0.8 0.4 0.075],...
'String','Enable/disable edit field 1',...
'Value',1,'Callback',@radio_callback);
for n=1:3
h.input(n)=createTextAndEdit(f,[0.2 0.8-n*0.1 0.4 0.075],n);
end
guidata(f,h);%store struct to GUI figure
function radio_callback(obj,evnt)
h=guidata(obj);%retrieve struct
distance=h.input(1).pos(2)-h.input(2).pos(2);
if h.radio.Value
%move everything down and show h.input(1)
distance=-1*distance;
vis='On';
else
%move everything up and hide h.input(1)
distance= 1*distance;
vis='Off';
end
for n=2:numel(h.input)
%move axis and edit field down/up
pos=h.input(n).ax.Position;
pos(2)=pos(2)+distance;
h.input(n).ax.Position=pos;
pos=h.input(n).edit.Position;
pos(2)=pos(2)+distance;
h.input(n).edit.Position=pos;
end
h.input(1).label.Visible=vis;
h.input(1).edit.Visible=vis;
end
function s=createTextAndEdit(parent,pos,num)
%create an axis with a text label and an edit uicontrol
s=struct;s.pos=pos;
AxisPercentage=40;%width taken up by axis
pos_ax=pos;pos_edit=pos;
pos_ax(3)=pos_ax(3)*AxisPercentage/100;
pos_edit(3)=pos(3)-pos_ax(3);pos_edit(1)=pos_edit(1)+pos_ax(3);
s.ax=axes('Parent',parent,...
'Units','Normalized','Position',pos_ax,...
'Visible','off');
try s.ax.Toolbar.Visible = 'off';end %#ok<TRYNC>
s.label=text(1,0.5,...%these are the coordinates within the axes
sprintf('edit field %d ',num),'Parent',s.ax,...
'HorizontalAlignment','Right');
s.edit=uicontrol('Parent',parent,'Style','Edit',...
'Units','Normalized','Position',pos_edit,...
'String','0','HorizontalAlignment','Right');
end

More Answers (1)

Mohammad Sami
Mohammad Sami on 19 Mar 2020
If you are using app designer something like this will work for you.
Essentially you can have an entire row or column of your grid set to 0 by a callback.
Do note this will affect some of your layout depending how you manage your layout.
f = uifigure;
g = uigridlayout('ColumnWidth',{'1x' '1x' '1x'},'RowHeight',{'1x' 50 50 50 '1x'},'Parent',f);
e1 = uieditfield(g);
e1.Layout.Column = 2;
e1.Layout.Row = 3;
e2 = uieditfield(g);
e2.Layout.Column = 2;
e2.Layout.Row = 4;
b = uibutton("state",'Parent',g,'Value',1);
b.Layout.Column = 2;
b.Layout.Row = 2;
a1 = uiaxes(g);
a1.Layout.Column = [1 3];
a1.Layout.Row = 1;
a2 = uiaxes(g);
a2.Layout.Column = [1 3];
a2.Layout.Row = 5;
hide = @(src,event,varargin) set(g,'ColumnWidth',{'1x' '1x' '1x'},'RowHeight',{'1x' 50 src.Value*50 src.Value*50 '1x'});
b.ValueChangedFcn = hide;

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!