MATLAB: Get values from several edit boxes using a push button in GUI

editboxgetguipushbutton

Hello I have a GUI to calculate a variable called S, where S=q*E*B. I want the user the enter the values of q,E and B then click a push button calculate so that the push button will display the value in a edit box called S. I think I need to use the get function but I'm not sure how to do it. Any help is much appreciated

Best Answer

There should be a callback function for your button that will fire whenever the user presses it. The callback (in its default state) will look something like this (though may be named differently for you)
% --- 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)
Now you have to add code to it to perform the calculation and display the result in the S edit text widget.
First you need to get your q, E, and B values. I am going to assume that an edit text box exists for each and it is named according to the variable prefixed with 'edit' (you will probably have named them differently). The handle for any widget can be found in the handles structure that is third input to your callback. To get the values, you can do something like
q = str2num(char(get(handles.editq,'String')));
E = str2num(char(get(handles.editE,'String')));
B = str2num(char(get(handles.editB,'String')));
If all values are numeric (and so aren't empty) you can perform the calculation and set the data in the S edit text widget
if ~isempty(q) && ~isempty(E) && ~isempty(B)
S = q*E*B;
set(handles.editS,'String',num2str(S));
end
Note the conversions from a string to a number and then back again. Try the above and see what happens!