Azzera filtri
Azzera filtri

Reference to non-existent field error occurring

7 visualizzazioni (ultimi 30 giorni)
Aaron Smith
Aaron Smith il 29 Mag 2017
Commentato: Jan il 2 Giu 2017
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 Commenti
Stephen23
Stephen23 il 29 Mag 2017
@Aaron Smith: please edit your question and show us the complete error message. This means all of the red text.
Aaron Smith
Aaron Smith il 29 Mag 2017
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

Accedi per commentare.

Risposte (1)

Jan
Jan il 29 Mag 2017
Modificato: Jan il 2 Giu 2017
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 Commenti
Aaron Smith
Aaron Smith il 2 Giu 2017
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 il 2 Giu 2017
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.

Accedi per commentare.

Categorie

Scopri di più su Interactive Control and Callbacks in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by