필터 지우기
필터 지우기

Reference to non-existent field error occurring

조회 수: 6 (최근 30일)
Aaron Smith
Aaron Smith 2017년 5월 29일
댓글: Jan 2017년 6월 2일
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