Call back function for state button in matlab gui
1 view (last 30 days)
Show older comments
Hi, actually I am new in GUI want to design a state button which have function like when I will press it will change it's color green and pass the value '1' to ECU and depress then back to it's original color grey and pass the value'0' to ECU. Plz help me
9 Comments
Rik
on 15 Mar 2022
It sounds like you don't really know how to solve this problem without a GUI. That is the first step. Since I only partly recognize the terms you're using, I'm afraid I can't help you with that part. Once you're at the stage where you only need your GUI to collect inputs, I may be able to help you.
Accepted Answer
Adam Danz
on 15 Mar 2022
Edited: Adam Danz
on 15 Mar 2022
There are a few short steps you need to take to implement this in App Designer. I've attached a demo app you can follow.
Set up the state button
- Add the UIButton in AppDesigner if you haven't done so already.
- Add the ButtonValueChangedFcn callback to the UIButton (see Step 1 in this answer for an illustration).
Define button actions
Set up your app to store the original button color
Your goal is to turn the button green when pressed (state button value 1) and to turn it back to its original color when de-pressed (state button value 0). You'll need to store the original UIButton color value when the app opens.
- Add a private property to your app that stores the original button color. The property name in my demo app is originalButtonColor
- Add a startup function to your app. The function will store the original state button color within the new property you created.
% Code that executes after component creation
function startupFcn(app)
% Set default button color
app.Button.Value = false;
app.originalButtonColor = app.Button.BackgroundColor;
end
Toggle the button color and send 1/0 values when button is pressed or de-pressed
You're almost done. I don't know what ECU is in your question but you can easily adapt my demo app to your needs. In the attached demo app, the following ButtonValueChanged function changes the button color to green and sends the value of '1' to a textbox when the state button is pressed. When de-pressed, the state button color returns to defaul and the value of '0' is passed to the textbox.
% Value changed function: Button
function ButtonValueChanged(app, event)
value = app.Button.Value;
if value % Button pressed
app.Button.BackgroundColor = [0 1 0]; % Green
app.TextArea.Value = '1';
else % Button de-pressed
app.Button.BackgroundColor = app.originalButtonColor;
app.TextArea.Value = '0';
end
end
2 Comments
Adam Danz
on 16 Mar 2022
Thanks for the explanation, Kuber. Sounds like you just need to change the part of my demo that sends 1/0 to the text box and instead, send it to your controller. Sounds interesting!
More Answers (0)
See Also
Categories
Find more on Develop Apps Using App Designer 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!