Reference to non-existent field during GUI OpeningFcn

4 Ansichten (letzte 30 Tage)
Matthew Freeland
Matthew Freeland am 26 Apr. 2012
In the OpeningFcn call of my GUI, I set initial values for various edit textboxes, a listbox, and checkboxes. When I launch the GUI, I get this error:
??? Reference to non-existent field 'thrust'.
Error in ==> Engine_GUI_v3>Engine_GUI_v3_OpeningFcn at 168
set(handles.thrust,'String',thrust)
Error in ==> gui_mainfcn at 221
feval(gui_State.gui_OpeningFcn, gui_hFigure, [], guidata(gui_hFigure), varargin{:});
Error in ==> Engine_GUI_v3 at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==> Kernel_Main_GUI>enginebutton_Callback at 341
Engine_GUI_v3
Error in ==> gui_mainfcn at 96
feval(varargin{:});
Error in ==> Kernel_Main_GUI at 42
gui_mainfcn(gui_State, varargin{:});
Error in ==>
@(hObject,eventdata)Kernel_Main_GUI('enginebutton_Callback',hObject,eventdata,guidata(hObject))
??? Error while evaluating uicontrol Callback
I have checked the tag for that particular edit text, and it is correct. When I go into debug mode to see what happens, I do not encounter this error. Any help would be greatly appreciated!
[Merged information from comment]
Hi Doug,
Thanks for taking the time to look into this. Here is my code for the opening function:
function Engine_GUI_v3_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Engine_GUI_v3 (see VARARGIN)
% Choose default command line output for Engine_GUI_v3
handles.output = hObject;
setappdata(0,'hEngineGui',gcf);
hEngineGui=getappdata(0,'hEngineGui');
hMainGui=getappdata(0,'hMainGui');
TcData=getappdata(hMainGui,'TcData');
%Read in Engine Data and store it in the Engine GUI
FileDBEngines='dB\engineDB.db';
[fid,message] = fopen(FileDBEngines, 'r');
if fid == -1
disp(strcat(FileDBEngines,' ',message))
end
while feof(fid) == 0
line = fgetl(fid);
evalc([line]);
end
fclose(fid);
setappdata(hEngineGui,'engineDB',engineDB);
%Make composite list (enginename - versionname)
engine_names=fieldnames(engineDB);
k=1;
for i=1:length(engine_names)
eval(['version_names=fieldnames(engineDB.' engine_names{i} ');'])
for j=1:length(version_names)
composite_list{k,1}=[engine_names{i} ' - ' version_names{j}];
composite_list{k,2}=i;
composite_list{k,3}=j;
k=k+1;
end
end
setappdata(hEngineGui,'composite_list',composite_list)
set(handles.listbox1,'String',composite_list(:,1))
%populate engine list
index=find(strcmp(composite_list(:,1),[TcData.Engine.engineName ' - ' TcData.Engine.engineVersion]));
set(handles.SelectedEngineTxt,'String',TcData.Engine.engineName)
set(handles.SelectedVersionTxt,'String',TcData.Engine.engineVersion)
if isempty(index)%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%engine not found in engineDB, show custom engine dialogue
index=1;
set(handles.listbox1,'Value',index)
set(handles.SelectedDescription,'String','')
%get engine info from TcData
m_core=TcData.Engine.m_core;
m_fan=TcData.Engine.m_fan;
m_fuel=TcData.Engine.m_fuel;
T_core=TcData.Engine.T_core;
T_fan=TcData.Engine.T_fan;
r_core=TcData.Engine.r_core;
r_fan=TcData.Engine.r_fan;
if isfield(TcData.Engine,'r_Inlet_Fan')
r_inlet_fan=TcData.Engine.r_Inlet_Fan;
else
r_inlet_fan=max([r_core r_fan]);
end
if isfield(TcData.Engine,'r_Bellmouth')
r_bellmouth=TcData.Engine.r_Bellmouth;
else
r_bellmouth=1.56348*r_inlet_fan;
end
nu_core=TcData.Engine.nu_core;
nu_fan=TcData.Engine.nu_fan;
thrust=TcData.Engine.thrust;
%display engine picture
image(imread(strcat('images/Custom Engine.jpg')))
axis off
%enable 'save' button
else%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%engine found in engineDB, get info from dB
set(handles.listbox1,'Value',index)
i=composite_list{get(handles.listbox1,'Value'),2};
j=composite_list{get(handles.listbox1,'Value'),3};
engine_names=fieldnames(engineDB);
selected_engine=engine_names{i};
version_names=eval(['fieldnames(engineDB.' selected_engine ');']);
selected_version=version_names{j};
set(handles.SelectedDescription,'String',eval(['engineDB.' selected_engine '.' selected_version '.description']))
%get engine info from engineDB
path=['engineDB.' selected_engine '.' selected_version];
m_core=eval([path '.m_CORE']);
m_fan=eval([path '.m_FAN']);
m_fuel=eval([path '.m_F']);
T_core=eval([path '.T_CORE']);
T_fan=eval([path '.T_FAN']);
r_core=eval([path '.r_CORE']);
r_fan=eval([path '.r_FAN']);
r_inlet_fan=eval([path '.r_Inlet_Fan']);
r_bellmouth=eval([path '.r_Bellmouth']);
nu_core=eval([path '.nu_CORE']);
nu_fan=eval([path '.nu_FAN']);
thrust=eval([path '.F']);
%display engine picture
image(imread(strcat('images/',selected_engine,'.jpg')))
axis off
%disable 'save' button
end%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
guidata(hObject, handles);
%fill in the blanks
set(handles.mcore,'String',m_core)
set(handles.mfan,'String',m_fan)
set(handles.mfuel,'String',m_fuel)
set(handles.Tcore,'String',T_core)
set(handles.Tfan,'String',T_fan)
set(handles.rcore,'String',r_core)
set(handles.rfan,'String',r_fan)
set(handles.rinletfan,'String',r_inlet_fan)
set(handles.rbellmouth,'String',r_bellmouth)
set(handles.nucore,'String',nu_core)
set(handles.nufan,'String',nu_fan)
set(handles.thrust,'String',thrust)
set(handles.thrust_klbf,'String',thrust*0.224808943/1000)
%airmeter design rule checkbox
if abs(1.56348*r_inlet_fan-r_bellmouth)<1e-3
set(handles.BellmouthCheckbox,'Value',1)
set(handles.rbellmouth,'Enable','off')
else
set(handles.BellmouthCheckbox,'Value',0)
set(handles.rbellmouth,'Enable','on')
end
%shade unused items
if (T_fan==0)||(m_fan==0)
set(handles.mfan,'BackgroundColor',[0.5 0.5 0.5])
set(handles.Tfan,'BackgroundColor',[0.5 0.5 0.5])
set(handles.rfan,'BackgroundColor',[0.5 0.5 0.5])
set(handles.nufan,'BackgroundColor',[0.5 0.5 0.5])
else
set(handles.mfan,'BackgroundColor','w')
set(handles.Tfan,'BackgroundColor','w')
set(handles.rfan,'BackgroundColor','w')
set(handles.nufan,'BackgroundColor','w')
end
%uncheck calculate thrust
set(handles.thrust_checkbox,'Value',0)
guidata(hObject, handles);
% UIWAIT makes Engine_GUI_v3 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
I should mention that the error is happening at "set(handles.thrust,'String',thrust)"...which is a pretty arbitrary location. All the other set(handles.etc) commands before that work, so I am at a loss as to why for some reason the handles structure does not include the edit text called thrust.
  2 Kommentare
Walter Roberson
Walter Roberson am 26 Apr. 2012
I'd be interested to find out: if you put a call to
drawnow()
just before the line that gives the error, then does the problem go away.
Walter Roberson
Walter Roberson am 26 Apr. 2012
If your engine data was all put into one structure, then you could use dynamic field names instead of eval() ...

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Doug Hull
Doug Hull am 26 Apr. 2012
Without looking at the code, I can not say why this would happen.
Without seeing code, I can offer alternate ways of accomplishing this. Can you set the strings in the property inspector for the object in GUIDE? Can you set the strings in the create function for the relevant objects?

Jan
Jan am 26 Apr. 2012
BTW, all your eval commands are not useful. They impede the debugging and reduce the processing speed. Example:
path = ['engineDB.' selected_engine '.' selected_version];
m_core = eval([path '.m_CORE']);
Better:
pathValue = engineDB.(selected_engine).(selected_version);
m_core = pathValue.m_CORE;
path is an important Matlab command. Shadowing it bvy a local variable is a bad idea.
Another example:
eval(['version_names=fieldnames(engineDB.' engine_names{i} ');'])
Better:
version_names = fieldnames(engineDB.(engine_names{i}));
A side-effect of avoiding the eval 's: The dynamically created variables can confuse Matlab in non-debug mode, while during debugging everything seems to run fine.
Nevertheless, I assume the error is caused by anything else here. Please try to update the handles struct:
handles = guidata(hObject);

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by