Search code examples
matlabuser-interfacevariablesworkspace

clear workspace of GUI matlab


I have a new button in my GUI here is its callback:

function ptlNew_ClickedCallback(hObject, eventdata, handles)  
% hObject    handle to ptlNew (see GCBO)  
% eventdata  reserved - to be defined in a future version of MATLAB  
% handles    structure with handles and user data (see GUIDATA)  
cla(handles.axes1,'reset');  
clear variables  

but the last line doesn't work
what can I do to clear workspace of my GUI without closing it to feed up memory?
my variables are in handles structure
for example if I have variable X, I should access it in form handles.X
I made my variables properties of handle because I wanted to access them in more than one callback function and I didn't want to make them Global

Also I tried to clear a specific variable. so instead of clear variables I used this line of code
clear handles.FiducialPixels
But it doesn,t work too and when I try to access it in the next line by code:
handles.FiducialPixels
it is printed on the Command Window quitely as it was before
I'm really confused what can I do? The interesting issue is that line clear variables or clear handles.FiducialPixels don't make any error or warning but they do their task!

As Li-aung Yip answered I changed the call back to this form:

function ptlNew_ClickedCallback(hObject, eventdata, handles)
names = fieldnames(handles);
rmfield(handles,'FiducialPixels');
names = fieldnames(handles)
handles.FiducialPixels  

but this is what appeares on the command line running lines 4 and 5:

names =

'figure1'
'pnlControlPointsImageCoordinates'
'uitoolbar1'
'pnlCalculateParameters'
'mnuInteriorOrientation'
'btnLoad2'
'edtImageFile'
'text17'
'edtInteriorOrientation'
'edtF'
'text16'
'edtYPPA'
'text15'
'edtXPPA'
'text14'
'text13'
'btnBrowseLensDistortion'
'edtLensDistortion'
'text12'
'btnBrowseInteriorFile'
'text11'
'axes2'
'uitoggletool3'
'uitoggletool2'
'uitoggletool1'
'ptlNew'
'txtCalculation'
'btnJumpTo'
'btnLoad'
'edtImagePath'
'text9'
'btnSaveParameters'
'btnCalculate'
'edtNumber'
'text8'
'uitable1'
'btnGinput'
'btnBrowse'
'edtFilePath'
'text1'
'btngrpTypeOfOrientation'
'axes1'
'mnuItemControlPointsImageCoordinates'
'mnuItemCalculateParameters'
'rdbtnProjective'
'rdbtnAffine'
'rdbtnConformal'
'output'
'TableData'
'TableDataEmpty'
'Matrix'
'FiducialImageCoordinates'
'dataH'
'FiducialPixels'
'flag'
'X'

592 handles.FiducialPixels

ans =

1.0e+003 *

7.5294    3.8246
0.1357    3.8723
3.9200    0.2311
3.8882    7.4499
7.4817    0.2311
0.2152    7.5294
0.2311    0.2311
7.4658    7.4817

You see handles.FiducialPixels still exists
Even if you use guidata(hObject,handles) after the third line, the result will be the same


Solution

  • If you want to delete absolutely every workspace variable, including all workspace (global) variables created by your script, and those variables created at the command prompt and those variables created by other scripts, just use:

    clear
    

    Using clear (every variable) is considered impolite, though. This is because a user might be doing some work on the command line before calling your script. When your script calls clear, all of his work is erased (forever!)


    If you want to delete handles.FiducialPixels - that is, a particular element of the handles struct - then clear is not what you want. Instead, use the rmfield() function, which is designed for deleting fields from structs.

    Homework: read the documentation pages for struct, and the functions for dealing with structs: cell2struct | deal | fieldnames | getfield | isfield | isstruct | namelengthmax | orderfields | rmfield | setfield | struct2cell | substruct.


    Edit(1): rmfield() does work. You are just not using it correctly.

    >> figure
    >> handles.h1 = uicontrol
    
    handles = 
    
        h1: 329.0011
    
    >> handles.h2 = uicontrol
    
    handles = 
    
        h1: 329.0011
        h2: 0.0016
    
    >> handles = rmfield(handles, 'h1')
    
    handles = 
    
        h2: 0.0016
    

    From doc rmfield:

    rmfield

    Remove fields from structure

    Syntax

    s = rmfield(s, 'fieldname')

    s = rmfield(s, fields)

    Description

    s = rmfield(s, 'fieldname') removes the specified field from the structure array s.

    s = rmfield(s, fields) removes more than one field at a time. fields is a character array of field names or cell array of strings.

    Note that the calling convention s = rmfield(...) indicates that rmfield returns something. In this case, the original structure s is not modified. Instead, rmfield() returns a new struct, without the fields you specified for deletion.