Getting 'Reference to non-existent field' Error

10 次查看(过去 30 天)
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)

采纳的回答

Matt Fig
Matt Fig 2012-11-22
编辑:Matt Fig 2012-11-22
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 个评论
Akhilesh Thakur
Akhilesh Thakur 2017-7-26
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 2017-7-26
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.

请先登录,再进行评论。

更多回答(1 个)

amir aslam
amir aslam 2019-6-17
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;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

类别

Help CenterFile Exchange 中查找有关 Desktop 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by