Reference to non-existent field 'a'.

50 views (last 30 days)
I was trying to fetch a string variable(a)and set it to another variable(b) using GUI .'a' is the tag for user input value and 'b' is output variable to be displayed.Please find the below code and error.
function fetch_Callback(hObject, eventdata, handles)
% hObject handle to fetch (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%x = handles.a;
handles = guidata(hObject);
a = str2double(get(handles.a,'String'));
set(handles.b,'String',a);
guidata(hObject,handles);
ERROR:
fetchvariable
Reference to non-existent field 'a'.
Error in fetchvariable>fetch_Callback (line 106)
A = str2double(get(handles.a,'String'));
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in fetchvariable (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)fetchvariable('fetch_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
  4 Comments
Jakob B. Nielsen
Jakob B. Nielsen on 31 Jan 2020
function fetch_Callback(hObject, eventdata, handles)
% hObject handle to fetch (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%x = handles.a;
handles = guidata(hObject);
keyboard
a = str2double(get(handles.a,'String'));
set(handles.b,'String',a);
guidata(hObject,handles);
Put in a keyboard break, run your code as you would, and then type handles in the command window. This will give you a list of structure fields contained within handles. Then it is easier to see what is going on "inside" the function. In particular, see if a exists - because it would appear, from your error message, that it does not.

Sign in to comment.

Answers (1)

Constantino Carlos Reyes-Aldasoro
This error is arising from trying to read a field from a structure, when the field is not existing for example
>> example.a = 1
example =
struct with fields:
a: 1
>> example.b=2
example =
struct with fields:
a: 1
b: 2
>> c=example.c
Reference to non-existent field 'c'.
>> c=example.b
c =
2
So, you are trying to read 'a' from the handles, when it is not present there. That is what you have to sort. If you want to know which fields are present in your structure try:
>> fieldnames(example)
ans =
2×1 cell array
{'a'}
{'b'}
and to test if one exists in particular
>>
>> isfield(example,'a')
ans =
logical
1

Community Treasure Hunt

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

Start Hunting!

Translated by