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

5 views (last 30 days)
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

Accepted Answer

Geoff Hayes
Geoff Hayes on 17 Jun 2014
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!
  5 Comments
racharla
racharla on 5 Mar 2018
please post from starting that is how to make matlab understand that 'editq' is for q value
Geoff Hayes
Geoff Hayes on 5 Mar 2018
racharla - please clarify your comment as I'm not clear on what you are asking for.

Sign in to comment.

More Answers (1)

Arshavin Hasegava
Arshavin Hasegava on 17 Jul 2020
Hi
I have found a way for assigning a push button in GUI for several inputs edit text. I mean if you want to update your several variables in Workspace using only a push button, you should first to the push button callback function and then write all your variable you would like to update as follow:
x=str2double(get(handles.input1, 'string'));
y=str2double(get(handles.input2, 'string'));
z=str2double(get(handles.input3, 'string'));
then you should assign each of then to the unique "assignin'' function as follow:
assignin('base', 'x', x);
assignin('base', 'y', y);
assignin('base', 'z', z);
After runing you GUI code and enter new values in your edit textbox and then press the corresponding push button you had defined, your variables will become update in the workspace.
It is worth to note that, if you have new variable, you should first define it in your workspace.

Categories

Find more on Interactive Control and Callbacks 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!