How do I debug this program?

This enclosed BMI program does not work correctly for all parameters. For example, a person that is 6'2" tall and weighs 194 lbs the calculated BMI is 24.9, but the program shows the weight status as "obese". I need to debug this program, but don't understand what the bug is or how to fix it.
function varargout = BMI(varargin)
% BMI MATLAB code for BMI.fig
% BMI, by itself, creates a new BMI or raises the existing
% singleton*.
%
% H = BMI returns the handle to a new BMI or the handle to
% the existing singleton*.
%
% BMI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in BMI.M with the given input arguments.
%
% BMI('Property','Value',...) creates a new BMI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before BMI_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to BMI_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help BMI
% Last Modified by GUIDE v2.5 15-Nov-2012 21:11:46
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @BMI_OpeningFcn, ...
'gui_OutputFcn', @BMI_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before BMI is made visible.
function BMI_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 BMI (see VARARGIN)
% Choose default command line output for BMI
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes BMI wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = BMI_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
function feet_Callback(hObject, eventdata, handles)
% hObject handle to feet (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of feet as text
% str2double(get(hObject,'String')) returns contents of feet as a double
% --- Executes during object creation, after setting all properties.
function feet_CreateFcn(hObject, eventdata, handles)
% hObject handle to feet (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function inch_Callback(hObject, eventdata, handles)
% hObject handle to inch (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of inch as text
% str2double(get(hObject,'String')) returns contents of inch as a double
% --- Executes during object creation, after setting all properties.
function inch_CreateFcn(hObject, eventdata, handles)
% hObject handle to inch (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function weight_Callback(hObject, eventdata, handles)
% hObject handle to weight (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of weight as text
% str2double(get(hObject,'String')) returns contents of weight as a double
% --- Executes during object creation, after setting all properties.
function weight_CreateFcn(hObject, eventdata, handles)
% hObject handle to weight (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
feet = str2num(get(handles.feet, 'string'));
inch = str2num(get(handles.inch, 'string'));
weight = str2num(get(handles.weight, 'string'));
bmi = weight*703/(feet*12+inch)^2;
set(handles.BMI, 'string', sprintf('%.1f',bmi));
if bmi<18.5
set(handles.status, 'string', 'Underweigth');
elseif bmi <= 24.9 && bmi>=19
set(handles.status, 'string', 'Normal');
elseif bmi > 25 && bmi <=29.9
set(handles.status, 'string', 'Overweight');
else
set(handles.status, 'string', 'Obese');
end

Risposte (1)

Note that with your structure of if/elseif you do not need to test for the lower part of each range:
if bmi < 18.5
elseif bmi <= 25
elseif bmi < 30
else
end
Notice I did not use 24.9 or 29.9 as you were missing out on all values greater than 24.9 and less than 25. MATLAB does not calculate to only single digits, remember.

Richiesto:

il 3 Dic 2012

Community Treasure Hunt

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

Start Hunting!

Translated by