1

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.

Robert Seifert
  • 25,078
  • 11
  • 68
  • 113
Jeiman
  • 1,121
  • 9
  • 27
  • 50

2 Answers2

3

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.

yuk
  • 19,098
  • 13
  • 68
  • 99
  • It is stating too many input arguments. I have basically copied the code from the example given in the link you posted and called it accordingly to 'closereq'. But how do i wrap this code around a button ? – Jeiman Feb 23 '12 at 18:05
  • Just add code to close the figure to pushbutton callback function. The figure will run `CloseRequestFcn` before it will be actually closed. – yuk Feb 23 '12 at 19:10
  • Here is a line you can insert to `pushbutton1_Callback` function to close the figure: `close(get(hObject,'Parent'))` – yuk Feb 23 '12 at 19:21
  • 1
    On the other hand, if you want the button to do something before closing the figure, but don't want to do it with standard closing, then just put those actions to pushbutton callback, not to `CloseRequestFcn`. – yuk Feb 23 '12 at 19:23
  • Another query, how do I allow a button to execute a script? Basically i would like to create a button in which that it states, `'Click the button to continue?'` and it executes a script file that contains other method to do something else ? Is this possible ? – Jeiman Feb 23 '12 at 20:16
  • 1
    You can do it, but you will have a problem with **variable scope**. The script will run in the scope of callback function and cannot access variables in MATLAB base workspace. You can pass the variables you need with `handles` parameter, or you can call script with `evalin`, or (the worst) define the variables global. It's actually a large issue, worth a separate question. Try to google or search SO for other similar questions. – yuk Feb 23 '12 at 21:24
0

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.

HeinrichStack
  • 694
  • 3
  • 8
  • 28