Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Getting 'Reference to non-existent field' Error

Asked by Xavier Abram on 22 Nov 2012

I have this code I run and i cant set my button properties using the callback at the end of the script. I tried it with an another code with the same parent buttons/figures relation and it did work. Now I want to understand what is wrong with those lines and I dont seem to find it. Teach me please! It keeps on telling me Reference to non-existent field...

Thanks in advance

function [] = HandlingGUIReal()
%% BACKGROUND FIGURE
Handling.Background = figure('units','pixels',...
    'position',[200 75 800 600],...
    'menubar','none',...
    'name','Problem Example');
Handling.ClosePB = uicontrol('style','pushbutton',...
    'units','pixels',...
    'Parent',Handling.Background,...
    'position',[550 15 70 22],...
    'string','Close',...
    'callback',{@ClosePB_callback,Handling});
%% DATA PANEL
Handling.DataPanel = uipanel('units','pixels',...
    'position', [50 450 250 50],...
    'Title','Data',...
    'Parent',Handling.Background);
Handling.LoadDataPB = uicontrol('style','pushbutton',...
    'units','pixels',...
    'Parent',Handling.DataPanel,...
    'position',[31 12 69 22],...
    'string','Load Data',...
    'callback',{@LoadDataPB_callback,Handling});
%% TEMPLATE PANEL
Handling.TemplatePanel = uipanel('units','pixels',...
    'position', [50 350 250 100],...
    'Title','Template',...
    'parent',Handling.Background);
Handling.LoadTemplatePB = uicontrol('style','pushbutton',...
    'units','pixels',...
    'Parent',Handling.TemplatePanel,...
    'position',[27 15 85 23],...
    'string','Load Template',...
    'Enable', 'off');
Handling.CreateTemplatePB = uicontrol('style','pushbutton',...
    'units','pixels',...
    'Parent',Handling.TemplatePanel,...
    'position',[130 15 90 23],...
    'string','Create Template',...
    'Enable', 'off');
Handling.TemplatePUM = uicontrol('style','popupmenu',...
    'units','pixels',...
    'Parent',Handling.TemplatePanel,...
    'string','   ',...
    'position',[30 50 95 22],...
    'Enable', 'off');
function LoadDataPB_callback(varargin)
% Callback for pushbutton.
Handling = varargin{3};  % Get the structure.
Handling.DataRunFigure = figure('units','pixels',...
    'position',[120 120 750 400],...
    'name','Data Run Definition Tool');
Handling.OkPB = uicontrol('style','pushbutton',...
    'units','pixels',...
    'Parent',Handling.DataRunFigure,...
    'position',[110 25 80 22],...
    'string','OK',...
    'callback',{@OKDataPB_callback,Handling});
function OKDataPB_callback(varargin)
% Callback for pushbutton.
Handling = varargin{3};  % Get the structure.
set(Handling.LoadTemplatePB,'Enable','on')
set(Handling.CreateTemplatePB,'Enable','on')
set(Handling.TemplatePUM,'Enable','on')
close(Handling.DataRunFigure)

0 Comments

Xavier Abram

Products

1 Answer

Answer by Matt Fig on 22 Nov 2012
Edited by Matt Fig on 22 Nov 2012
Accepted answer

At the time you set the callbacks, those fields did not exist in the structure. As the very last line of your main function, put:

set(Handling.ClosePB,'callback',{@ClosePB_callback,Handling})
set(Handling.LoadDataPB,'callback',{@LoadDataPB_callback,Handling})

Thus the whole structure has been created and your function can access the needed fields. Of course then you can also take out the line in the uicontrol creation where you assigned the callback, since you are doing it at the end...

4 Comments

Matt Fig on 23 Nov 2012

That is a strange thing to say, Xavier. The reason why it is a strange thing to say is that when you make the changes I suggest above, the code does what you say and also doesn't error any more....

Xavier Abram on 26 Nov 2012

okay, is it because when the callback is assigned to Handling.LoadDataPB the others are not created yet and thus the callback doesn't recognize the other properties to modify?

Matt Fig on 26 Nov 2012

Something like that. It is not that the callback doesn't recognize the other fields of the structure, it is just the way MATLAB works. On the line where the callback is created, S only had certain fields (the were created before that line). It is a copy of S, as it exists at the time of the assignment, that is passed to the callback.

So when you add more fields to the structure later in the code, they are not added to the copy of the structure passed to the callback earlier.

Matt Fig

Contact us