MATLAB: Project help, I need to submit the project a gui tommorow but i keep on getting errors

gui

function varargout = moment(varargin)
% MOMENT MATLAB code for moment.fig
% MOMENT, by itself, creates a new MOMENT or raises the existing
% singleton*.
%

% H = MOMENT returns the handle to a new MOMENT or the handle to
% the existing singleton*.
%
% MOMENT('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in MOMENT.M with the given input arguments.
%
% MOMENT('Property','Value',...) creates a new MOMENT or raises
% the existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before moment_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to moment_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 moment
% Last Modified by GUIDE v2.5 26-Jun-2016 19:18:43
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @moment_OpeningFcn, ...
'gui_OutputFcn', @moment_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 moment is made visible.
function moment_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 moment (see VARARGIN)
% Choose default command line output for moment
handles.output = hObject;
% Update handles structure

guidata(hObject, handles);
%initialize_gui(hObject, handles, false);
% UIWAIT makes moment wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = moment_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;
% --- Executes during object creation, after setting all properties.

function mass1_CreateFcn(hObject, eventdata, handles)
% hObject handle to mass1 (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 mass1_Callback(hObject, eventdata, handles)
% hObject handle to mass1 (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 mass1 as text
% str2double(get(hObject,'String')) returns contents of mass1 as a double
handles.mass1 = str2double(get(hObject, 'String'));
if isnan(mass)
set(hObject, 'String', 0);
errordlg('Input must be a number','Error');
end
% Save the new mass1 value
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function radiusb_CreateFcn(hObject, eventdata, handles)
% hObject handle to radiusb (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 radiusb_Callback(hObject, eventdata, handles)
% hObject handle to radiusb (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 radiusb as text
% str2double(get(hObject,'String')) returns contents of radiusb as a double
handles.radiusb = str2double(get(hObject, 'String'));
if isnan(radius)
set(hObject, 'String', 0);
errordlg('Enter a number','Error');
end
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function radius2b_CreateFcn(hObject, eventdata, handles)
% hObject handle to radius2b (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 radius2b_Callback(hObject, eventdata, handles)
% hObject handle to radius2b (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 radius2b as text
% str2double(get(hObject,'String')) returns contents of radius2b as a double
handles.radius2b= str2double(get(hObject, 'String'));
if isnan(radius2)
set(hObject, 'String', 0);
errordlg('Input must be a number','Error');
end
% Save the new radius2b value
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function lengthb_CreateFcn(hObject, eventdata, handles)
% hObject handle to lengthb (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 lengthb_Callback(hObject, eventdata, handles)
% hObject handle to lengthb (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 lengthb as text
% str2double(get(hObject,'String')) returns contents of lengthb as a double
handles.lengthb= str2double(get(hObject, 'String'));
if isnan(length)
set(hObject, 'String', 0);
errordlg('Input must be a number','Error');
end
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function lenghtab_CreateFcn(hObject, eventdata, handles)
% hObject handle to lenghtab (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 lenghtab_Callback(hObject, eventdata, handles)
% hObject handle to lenghtab (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 lenghtab as text
% str2double(get(hObject,'String')) returns contents of lenghtab as a double
handles.lenghtab= str2double(get(hObject, 'String'));
if isnan(lenghta)
set(hObject, 'String', 0);
errordlg('Input must be a number','Error');
end
%
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function lenghtbb_CreateFcn(hObject, eventdata, handles)
% hObject handle to lenghtbb (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 lenghtbb_Callback(hObject, eventdata, handles)
% hObject handle to lenghtbb (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 lenghtbb as text
% str2double(get(hObject,'String')) returns contents of lenghtbb as a double
handles.lenghtbb = str2double(get(hObject, 'String'));
if isnan(lenghtb)
set(hObject, 'String', 0);
errordlg('Enter a number','Error');
end
% Save the new lenghtbb value
guidata(hObject,handles)
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)

% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from popupmenu1
updateaxes(handles)
guidata(hObject,handles)
% --- Executes during object creation, after setting all properties.
function popupmenu1_CreateFcn(hObject, eventdata, handles)
% hObject handle to popupmenu1 (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
% --- Executes on button press in calculate.
function calculate_Callback(hObject, eventdata, handles)
% hObject handle to calculate (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
hoop=handles.mass1*(handles.radiusb.^2);
hollow=0.5*handles.mass1*(handles.radiusb.^2+handles.cradius2b.^2);
disk=(1/12)*handles.mass1*(handles.radiusb.^.2);
rectangle=(1/12)*handles.mass1*(handles.lenght.^2+handles.langhtbb.^2);
thin_rod=(1/12)*handles.mass1*(handles.lenghtb.^2)
thin_rod_2=(1/3)*handles.mass1*(handles.lenghtb.^2);
sphere=(2/5)*handles.mass1*(handle.radiusb.^2)
shere_shell=(2/3)*handles.mass1*(handle.radiusb.^2)
switch str
case 'Hoop'
set(handles.Answer, 'String',hoop);

Best Answer

You have a uicontrol with tag 'mass1', so GUIDE automatically creates handles.mass1 as the handle of that uicontrol .
If you happen to go through mass1_Callback then you replace handles.mass1 with the str2double() of the string that was stored there, leaving handles.mass1 as a numeric value that can no longer be used to reference the uicontrol. For example, reset_Callback would fail as you would try to set() the String property of the double that you had stored in handles.mass1 . But we can prove that you did not go through that callback, because that callback contains
if isnan(mass)
and mass is undefined in that context so you would have failed in that callback.
If you do not happen to go through mass1_Callback (or you crashed in that callback) then in calculate_Callback then the first thing you do is
hoop=handles.mass1*(handles.radiusb.^2);
which would be an attempt to multiply the handle of the uicontrol for mass1 by something. That is going to fail.
Morals of the story:
  • do not store values into the handles structure using the same name as one of your graphics objects
  • you need to plan for the case where the flow of control is not what you expect -- so if you are using mass1_Callback to store the numeric equivalent of the mass1 uicontrol string into the handles structure, then you need to have stored the default numeric value for that into handles (in case the user did not happen to go through that callback) or you need to disable all callbacks that rely on the numeric mass1 value until you have gone through the mass1_Callback
  • for simple numeric values like you have for your variables, a more typical style would be to just validate in the edit callback like mass1_Callback, verifying that the string represents a number within bounds but not storing the numeric value inside the handles structure. Then when you need the numeric value in your other callbacks, fetch the string and convert it. This ensures synchronization (the stored numeric value cannot disagree with the string if there is no stored numeric value), and keeps you from having to update the handles structure all the time -- the fewer items you have stored in the handles structure, the more efficient using the handles structure is. This is not the only possible way to create a GUI, but it does have some advantages.
Related Question