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

6 次查看(过去 30 天)
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

采纳的回答

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

请先登录,再进行评论。

更多回答(1 个)

Arshavin Hasegava
Arshavin Hasegava 2020-7-17
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.

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by