if pushutton1 is pressed, how to use the value associated with it for displaying result for pushbutton2 in GUI ?
1 次查看(过去 30 天)
显示 更早的评论
I am working on GUI. I have two pushbuttons, pushbutton1 and pushbutton2. Also I can give three input integer values. When pushbutton1 is pressed, it displays result (for instance, multiplication of three input values a*b*c). I want that if pushbutton1 is already pressed, and after pressing pushbutton2, it shoud hold the previous input value (only a) and I should be able to change other values b and c and thus display the result of a*b*c. I don't know how to hold value a and change other values b and c by pressing pushbutton2. Please help.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1036610/image.jpeg)
0 个评论
采纳的回答
Voss
2022-6-18
If you want to prevent the user from changing the value in the 'a' edit box, you can set the 'Enable' property of that edit box to 'off' or 'inactive'.
set(edit_a,'Enable','off') % disabled
% - or -
set(edit_a,'Enable','inactive') % disabled, but looks enabled
(Also, you may consider making pushbutton2 a togglebutton rather than a pushbutton, since you can use the Value property of a togglebutton to determine whether it is currently pressed or not. Using a checkbox may be an appropriate alternative as well.)
0 个评论
更多回答(1 个)
Image Analyst
2022-6-18
In the callback for button 1, record its pushed status and original value of a. Untested code:
% Get value of a from the edit field and save it as field of app.
app.a = app.edta.Value;
% Log that pushbutton 1 was clicked.
app.button1Clicked = true;
Now in your code for button 2 you can retrieve the value of a if button 1 was clicked. You can also change b and c in that callback if you want
if app.a
% Button 1 has been clicked already. Use original value for a
a = app.a;
else
% Button 1 has not yet been clicked. Get value from edit field.
a = app.edta.Value;
end
% Get current value of b and c from the edit field.
b = app.edtb.Value;
c = app.edtc.Value;
% Change b and c if you want
b = 5;
app.edtb.Value = b;
c = 20;
app.edtc.Value = c;
% Put product into the result edit field
app.edtResult.Value = a*b*c;
Not sure if you can use value with numbers. You might have to use the String property and str2double and num2str if it doesn't work.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!