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

조회 수: 3 (최근 30일)
Mahmoud
Mahmoud 2014년 6월 17일
답변: Arshavin Hasegava 2020년 7월 17일
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