Call back function for state button in matlab gui

1 Ansicht (letzte 30 Tage)
KUBER DWIVEDI
KUBER DWIVEDI am 13 Mär. 2022
Kommentiert: Adam Danz am 16 Mär. 2022
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 Kommentare
Rik
Rik am 15 Mär. 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.
KUBER DWIVEDI
KUBER DWIVEDI am 16 Mär. 2022
Thanku Rik for your response, sure I will back to you in future, if I will need any help regarding GUI.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Adam Danz
Adam Danz am 15 Mär. 2022
Bearbeitet: Adam Danz am 15 Mär. 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
  1. Add the UIButton in AppDesigner if you haven't done so already.
  2. 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.
  1. Add a private property to your app that stores the original button color. The property name in my demo app is originalButtonColor
  2. 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 Kommentare
KUBER DWIVEDI
KUBER DWIVEDI am 16 Mär. 2022
Thank you Adam for your response, it is working thanks again, and ECU means electronic control unit (Microcontroller). I want to control a water Flow Valve 'on and off so to control it I have used a controller and want to send the signal from GUI, like If the user pressed the button it is send the signal '1' to Microcontroller and the microcontroller will take action further to On the flow valve. So basically it is the part of communication between GUI and Microcontroller.
Adam Danz
Adam Danz am 16 Mär. 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!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Develop Apps Using App Designer finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by