Jeiman
Jeiman

Reputation: 1181

Add a button on a figure and to close the figure in MATLAB

I have a set of data that i have pulled out of the database. I have displayed them in a figure window, but i would like to have a button in which where it closes the figure window or does some other function to it.

This is the code that i have so far :

f = figure('Position',[200 200 250 500]); % size of the figure object
dat = listofPdb.Data;
set(f,'name','List of PDBs available','numbertitle','off') %renames the Title Figure
cnames = {'PDB-Codes'};
rnames = {};
t = uitable('Parent',f,'Data',dat,'ColumnName',cnames,... 
            'RowName',rnames,'Position',[100 100 95 350]);

Please advise.

Upvotes: 1

Views: 5767

Answers (2)

yuk
yuk

Reputation: 19870

You need to define CloseRequestFcn property of the figure:

set(f,'CloseRequestFcn', @closereq)

where closereq is a function what to do when figure is closed.

See Figure properties for more information and examples.


Update (after chat in comments):

For a pushbutton you can define the callback function just to close the figure (insert close(get(hObject,'Parent')) into pushbutton1_Callback) and the CloseRequestFcn will do the rest.

On the other hand, if you want the pushbutton to do something before closing the figure, but don't want to do it with standard closing, then just insert those actions to the pushbutton callback, not to CloseRequestFcn.

Upvotes: 3

HeinrichStack
HeinrichStack

Reputation: 694

Type guide and design your figure. Than place a pushbutton over it, right click -> closing function. And define the behaviour you want to have for closing the figure.

Upvotes: 0

Related Questions