user2868118
user2868118

Reputation: 43

Matlab Uitable On/Off

Please I have created a gui with two push buttons; one for showing uitable and the other for hiding the uitable. My Problem is that, the Hide push button does not set the uitable's visibility to off. The code I have is shown below. Any help why this is not working? Thank you.

function []=hide()
SCR = get(0,'Screensize');  % Get screensize.
S.fh = figure('color',[0.8 0.8 0.8],'numbertitle','off',...
          'units','pixels',...
          'position',[SCR(3)/2-500 ,300 , 650, 600],...
          'name','myTable',...
          'resize','on');
movegui(S.fh,'center');

S.pb(1)=uicontrol('style','push','units','pixels','position',...
[5 530 150 30],'string','Show Table','fontsize',12,...
'fontweight','bold'); 
S.pb(2)=uicontrol('style','push','units','pixels','position',...
[255 530 170 30],'string','Hide Table','fontsize',12,...
'fontweight','bold'); 
%Callbacks
set(S.pb(1),'callback',{@pb_call1,S});
set(S.pb(2),'callback',{@pb_call2,S});

%PushButtons Operation
function []=pb_call1(varargin)
S=varargin{3};
S.t=uitable('Parent',S.fh,'Data',magic(10));
end

end

function []=pb_call2(varargin)
S=varargin{3};
S.t=uitable('Parent',S.fh,'Data',magic(10));
set(S.t,'visible','off')
end

Upvotes: 0

Views: 662

Answers (1)

Notlikethat
Notlikethat

Reputation: 20924

In pb_call2() you're creating a new uitable, overwriting your handle to the old one then hiding the new one. Net result: the old table is still visible and now you don't have a handle to it.

In short, remove

S.t=uitable('Parent',S.fh,'Data',magic(10));

from pb_call2().

Bear in mind pb_call1() is creating a new uitable every time it is called too, so you'll run into a variant of this same problem soon enough. Better to create the table once at initialisation and just have the callbacks set properties on or off.

Upvotes: 1

Related Questions