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!
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Owen Hall
am 10 Dez. 2018
Kommentiert: Owen Hall
am 10 Dez. 2018
% --- 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.
0 Kommentare
Akzeptierte Antwort
Ken Atwell
am 10 Dez. 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.
Weitere Antworten (1)
Geoff Hayes
am 10 Dez. 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'));
Siehe auch
Kategorien
Mehr zu Whos 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!