Getting 'Reference to non-existent field' Error

7 views (last 30 days)
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)

Accepted Answer

Matt Fig
Matt Fig on 22 Nov 2012
Edited: Matt Fig on 22 Nov 2012
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...
  6 Comments
Akhilesh Thakur
Akhilesh Thakur on 26 Jul 2017
Even I was using callbacks of Keypress function and it throwed me error that: Reference to non-existent field 'patternkeypress'. The problem here is the callback is not created yet where my program runs. So the issue here is you have to handle callbacks before your actual program runs. Like in my case I force a keypress before my program so that it never fails.
Walter Roberson
Walter Roberson on 26 Jul 2017
If the user has to undertake operations before it becomes valid to invoke a particular callback, then the callback should be disabled until it becomes valid. In the case of uicontrol 'callback' property, that means set the 'enable' property to 'disable', and then change it to 'enable' as it becomes valid to invoke. For something like a keypressfcn it might mean setting the pickableparts property off.
Or... you could have a CreateFcn callback for the GUI element that holds the keypressfcn and that CreateFcn callback can initialize appropriate variables. But be careful with this: if you are loading a .fig such as one created by GUIDE, then the handles structure does not exist until after all of the CreateFcn have been called.
Now in the case of loading a .fig such as one created by GUIDE, no user-interaction function become callable until all of the CreateFcn have run -- not unless you stuck a pause() or uiwait() or waitfor() or drawnow() inside one of the CreateFcn. So what you need to do in the case of GUIDE is to go into your *OpeningFcn and put the assignments there: the handles structure has been created by then, but the figures has not been made visible.

Sign in to comment.

More Answers (1)

amir aslam
amir aslam on 17 Jun 2019
Teach me please! It keeps on telling me Reference to non-existent field...
clear; close all; clc;
addpath('lib')
testData = load('data.mat');
nnOptions = {};
modelNN = learnNN(testData.X, testData.y, nnOptions);
plotConfMat(modelNN.confusion_valid);
%% Predicting on Malware
rI = randi(size(testData.X, 1));
p = predictNN(testData.X(rI,:), modelNN);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
input_layer_size = size(X, 2);
layers = [input_layer_size, hiddenLayers, nrOfLabels];
modelNN.activationFn = activationFn;
modelNN.lambda = lambda;
modelNN.maxIter = maxIter;
modelNN.layers = layers;
modelNN.doNormalize = doNormalize;
X(isnan(X))=0;
Y = Y - min(Y) + 1;
m = size(X, 1);
validation_set_size = round(m*validPercent/100); % e.g. 10% for the validation
rand_indices = randperm(m);
X = X(rand_indices, :);
Y = Y(rand_indices);
X_valid = X(1:validation_set_size, :);
Y_valid = Y(1:validation_set_size);
X(1:validation_set_size,:) = [];
Y(1:validation_set_size) = [];
modelNN.X = X;
modelNN.Y = Y;
modelNN.X_valid = X_valid;
modelNN.Y_valid = Y_valid;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!