The function that makes the calculations for my code doesn't work
14 views (last 30 days)
Show older comments
Aneizka Albertorio
on 7 Feb 2022
Commented: Benjamin Thompson
on 8 Feb 2022
Greetings,
Recently I made GUI that allows the user to calculate a child's future height. In this GUI, I use three different models to calculate the height. However, the function that's in charge of performing the necessary calculations is not working and I don't understand why. I have watched videos and even read questions from others in the community but I still can't Identify was wrong. Two of the height models depend on the gender of the user. So I suspect that the function is failing on calling the values of the gender handle. Additionally, I also tried to create a bar graph with the child's current height, the child's future height, the father's height and mother's height but receive an error message. Here I attach a picture of my GUI and code.
function varargout = Height_calculator_v2(varargin)
% HEIGHT_CALCULATOR_V2 MATLAB code for Height_calculator_v2.fig
% HEIGHT_CALCULATOR_V2, by itself, creates a new HEIGHT_CALCULATOR_V2 or raises the existing
% singleton*.
%
% H = HEIGHT_CALCULATOR_V2 returns the handle to a new HEIGHT_CALCULATOR_V2 or the handle to
% the existing singleton*.
%
% HEIGHT_CALCULATOR_V2('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in HEIGHT_CALCULATOR_V2.M with the given input arguments.
%
% HEIGHT_CALCULATOR_V2('Property','Value',...) creates a new HEIGHT_CALCULATOR_V2 or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Height_calculator_v2_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Height_calculator_v2_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 Height_calculator_v2
% Last Modified by GUIDE v2.5 07-Feb-2022 00:37:40
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Height_calculator_v2_OpeningFcn, ...
'gui_OutputFcn', @Height_calculator_v2_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 Height_calculator_v2 is made visible.
function Height_calculator_v2_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 Height_calculator_v2 (see VARARGIN)
set(handles.panelgender, 'SelectionChangedFcn', @panelgender_SelectionChangedFcn);
panelgender_SelectionChangedFcn(handles.panelgender, struct('NewValue', handles.Male))
handles.gender=0;
% Choose default command line output for Height_calculator_v2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes Height_calculator_v2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = Height_calculator_v2_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;
% Hint: get(hObject,'Value') returns toggle state of Female
% --- Executes when selected object is changed in panelgender.
function panelgender_SelectionChangedFcn(hObject, eventdata, handles)
% hObject handle to the selected object in panelgender
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles=guidata(hObject);
switch get(eventdata.NewValue, 'Tag')
case 'Male'
handles.gender=1;
case 'Female'
handles.gender=0;
end
% Update handles structure
guidata(hObject, handles);
% --- Executes on selection change in Models.
function Models_Callback(hObject, eventdata, handles)
% hObject handle to Models (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
modelo=get(hObject, 'String');
Val=get(hObject, 'Value');
switch modelo{Val}
case 1
fh=str2double(get(handles.Fathers_height, 'String'));
mh=str2double(get(handles.mothers_height,'String'));
if handles.gender==1
h=((mh*13/12) + fh)/2;
elseif handles.gender==0
h=((fh*12/13) + mh)/2;
end
H=num2str(h);
handles.Future_height=H;
guidata(hObject, handles)
set(handles.Future_height, 'String', H)
case 2
hy=str2double(get(handles.Height_2,'String'));
h=hy*2;
H=num2str(h);
handles.Future_height=H;
guidata(hObject, handles)
set(handles.Future_height, 'String', H)
case 3
hy=str2double(get(handles.Height_2,'String'));
if handles.gender==1
h=22.7+1.37*hy;
elseif handles.gender==2
h=25.0+1.17*hy;
end
H=num2str(h);
handles.Future_height=H;
guidata(hObject, handles)
set(handles.Future_height, 'String', H)
end
Ch=str2double(get(handles.Current_height, 'String'));
h=str2double(get(handles.Future_height, 'String'));
y=[h,Ch;fh;mh];
bar(y)
xlabel('Individual')
ylabel('Height')
% Hints: contents = cellstr(get(hObject,'String')) returns Models contents as cell array
% contents{get(hObject,'Value')} returns selected item from Models
% --- Executes during object creation, after setting all properties.
function Models_CreateFcn(hObject, eventdata, handles)
% hObject handle to Models (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu 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 Future_height_Callback(hObject, eventdata, handles)
% hObject handle to Future_height (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 Future_height as text
% str2double(get(hObject,'String')) returns contents of Future_height as a double
% --- Executes during object creation, after setting all properties.
function Future_height_CreateFcn(hObject, eventdata, handles)
% hObject handle to Future_height (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 Start_Over.
function Start_Over_Callback(hObject, eventdata, handles)
% hObject handle to Start_Over (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.Name,'String','');
set(handles.AGE,'String','');
set(handles.Current_height,'String','');
set(handles.Fathers_height,'String','');
set(handles.Mothers_height,'String','');
set(handles.Future_height,'String','');
Error code
Unrecognized function or variable 'fh'.
Error in Height_calculator_v2>Models_Callback (line 257)
y=[h,Ch;fh;mh];
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in Height_calculator_v2 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)Height_calculator_v2('Models_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating UIControl Callback.
GUI


NOTE: Not all of the code of the entire project is included, only the parts where it is not working.
3 Comments
Voss
on 8 Feb 2022
Did you restart your GUI after taking out the part that overwrote handles.Future_height? You have to restart it to recover from a thing like that.
In any case, I wasn't saying that was the source of the error, just that it's not what you want to be doing, regardless.
However, upon closer inspection, it looks like the error may be you've got a comma where there should be a semicolon. Try changing this:
y=[H,Ch;fh;mh];
to this:
y=[H;Ch;fh;mh];
Accepted Answer
Benjamin Thompson
on 7 Feb 2022
The variable fh appears to be defined in only one case of the switch statement. But it is used after the switch statement regardless of which case might have been executed. fh should be defined to a good default value prior to the switch statement.
2 Comments
Benjamin Thompson
on 8 Feb 2022
Is this now fixed for you with the additional comments from user "_"? Please accept an answer or ask additional specific questions.
More Answers (0)
See Also
Categories
Find more on Array Geometries and Analysis 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!