Reference to non-existent field error occurring

3 次查看(过去 30 天)
Matlab is generating the 'reference to non-existent field' error for my code. It is refering to one of my handles. Online it appears that this occurs if the field does not exist in the structure at the time the callback is set. This is not true for the offending field in my code as it is declared in the opening function and I have used guidata to update the handles structure. Is there another reason why this error should be occurring?
Error while evaluating uicontrol Callback
Reference to non-existent field 'verticalBin'.
Error in new_window_2>pushbutton4_Callback (line 190)
set(handles.verticalBin, 'enable', 'on');
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in new_window_2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in @(hObject,eventdata)new_window_2('pushbutton4_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
  2 个评论
Stephen23
Stephen23 2017-5-29
@Aaron Smith: please edit your question and show us the complete error message. This means all of the red text.
Aaron Smith
Aaron Smith 2017-5-29
Done. It specifies the line where the error arises. Depending on which push button is used it will either be 116, 153 or 190 as the same line exists in all three

请先登录,再进行评论。

回答(1 个)

Jan
Jan 2017-5-29
编辑:Jan 2017-6-2
As mentioned in your other thread, your code would be much easier to read, if you use the auto-indentation.
The are some bugs in the code. E.g.:
% finishCellB = cell(length(FilesB)); Should be:
finishCellB = cell(1, length(FilesB));
Simplify your code:
if handles.option == 1 % If this button is pushed, Bin the corresponding file
handles = guidata(hObject);
binnedH = sum(handles.finishCellfull, 2);
handles.horizontalBin = stairs(binnedH);
guidata(hObject, handles);
elseif handles.option == 2 % If this button is pushed, Bin the corresponding file
handles = guidata(hObject);
binnedH = sum(handles.finishCellhalf, 2);
handles.horizontalBin = stairs(binnedH);
guidata(hObject, handles);
else % If this button is pushed, Bin the corresponding file
handles = guidata(hObject);
binnedH = sum(handles.finishCellquarter, 2);
handles.horizontalBin = stairs(binnedH);
guidata(hObject, handles);
end
Leaner:
handles = guidata(hObject);
if handles.option == 1
binnedH = sum(handles.finishCellfull, 2);
elseif handles.option == 2
binnedH = sum(handles.finishCellhalf, 2);
else
binnedH = sum(handles.finishCellquarter, 2);
end
handles.horizontalBin = stairs(binnedH);
guidata(hObject, handles);
Or even:
handles = guidata(hObject);
options = {'finishCellfull', 'finishCellhalf', 'finishCellquarter'};
field = options{handles.option};
handles.horizontalBin = stairs(sum(handles.(field), 2));
guidata(hObject, handles);
Less code, less chances for typos.
We still cannot run your code and therefore not reproduce the problem. And as in your other thread, I mentione, that you can examine what's going on using the debugger. Set breakpoints in the code and check, where the concerned field is existing and where it is missing. Then it will be a command in between, which has removed the field.
Can you confirm, that the field handles.verticalBin does exist inside the OpeningFcn in line 63? Set a breakpoint in this line to check this.
  29 个评论
Aaron Smith
Aaron Smith 2017-6-2
Yeah, I have been opening the figures by clicking on the .fig file. I just tried running it from the code and it works perfectly fine. I am not sure what the problem was
Jan
Jan 2017-6-2
As I said: Clicking on a FIG file opens the figure, but does not initialize e.g. the handles struct. Only running the M-file does this.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by