Why doesn't the callback I've written here seem to work? As far as I can tell all of my syntax is correct and I know that none of the pregenerated code has been altered. Thank's in advance!

1 view (last 30 days)
% --- Exe
cutes on button press in btnCalc.
function btnCalc_Callback(hObject, eventdata, handles)
% hObject handle to btnCalc (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
gasTank = str2num(get(handles.tankEdit, 'string')); % Assigns 'gasTank' to the text field containing the size of the gas tank
gasPrice = str2num(get(handles.priceEdit, 'string')); % Assigns 'gasPrice' to the text field containing the cost of gas per gallon
gasBought = str2num(get(handles.boughtEdit, 'string')); % Assigns 'gasBought' to the text field containing the amount of gas purchased
dist = str2num(get(handles.app.distEdit, 'string')); % Assigns 'dist' to the text field containing the distance traveled in miles
galUsed = gasTank - gasBought ; % Calculates amount of gas consumed since last odometer reset
mpg = dist / galUsed ; % Calculates miles per gallon over the distance traveled
cpm = gasPrice / mpg ; % Calculates the cost per mile for the user to travel the distance covered
set(handles.mpgEdit,'string',num2str(mpg)); % Displays calculated miles per gallon
set(handles.cpmEdit,'string',num2str(cpm)); % Displays cost per mile of travel
These are the error messages that the command window throws when I input values and run the code I've written into this callback :
Reference to non-existent field 'app'.
Error in mpgCalculator>btnCalc_Callback (line 177)
dist = str2num(get(handles.app.distEdit, 'string')); % Assigns 'dist' to the text field containing the distance traveled in miles
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in mpgCalculator (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)mpgCalculator('btnCalc_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.

Accepted Answer

Ken Atwell
Ken Atwell on 10 Dec 2018
The error message:
Reference to non-existent field 'app'.
is telling you that you are trying to access handles.app, that is, the "app" field of the "handles" struct, but "app" is NOT a field of "handles". Set a breakpoint at line 177 and run there. In the command window or Workspace window, examine what fields of "handles" are available -- may you just got the name wrong, or you forgot to added this field earlier to the "handles" struct.
  1 Comment
Owen Hall
Owen Hall on 10 Dec 2018
Thanks a bunch! I copy pasted this code from a similar program I was working on in app designer (which I also couldn't get to work) and I was trying to figure out how the variable names worked in the app designer. I thought I had removed all of the '.app' prefixes, but apparently not.

Sign in to comment.

More Answers (1)

Geoff Hayes
Geoff Hayes on 10 Dec 2018
Owen - in your line of code
dist = str2num(get(handles.app.distEdit, 'string'));
perhaps the app is unnecessary (it seems to be using your previous lines as an example and the error message of Reference to non-existent field 'app') and so you should be doing instead
dist = str2num(get(handles.distEdit, 'string'));

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!