Call back function for state button in matlab gui

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 个评论

Is the GUI a figure or uifigure? Do you use AppDesigner, GUIDE or create the GUI programmatically? What does "pass to ECU" mean?
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
First of all Thanku all for your response,I am using app designer for GUI and ECU means Electronic control unit simply say microcontroller in which there is a logic for flow Mode, so GUI interacts with microcontroller. I want when the user press the button then it is 'On' and change the button background color and pass the value 'Flow mode =1' to the microcontroller through CAN Protocol, then Flow mode will be activated and when the user depressed the button then it will back to its original color and pass the value 'Flow mode =0' to the microcontroller and as it receives the flow mode signal value '0' it will deactivate the flow mode.
How would you do this outside of a GUI?
I will map all the variables of GUI with microcontroller logic and create a Transfer buffer through CAN BUS and Vehicle Network Toolbox, which will communicate with GUI to Microcontroller application layer, and the application layer receives the signal from the GUI and will execute the logic accordingly.
For that, I need to write call-back functions to assign the value to the respected variable and transfer that value of the variable to the respective CAN-BUS transfer ID. Plz correct me if I am wrong and guide me on the right track, also what would be the structure of the callback function in this case.
And I in my case GUI is uifigure.
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.
Thanku Rik for your response, sure I will back to you in future, if I will need any help regarding GUI.

请先登录,再进行评论。

 采纳的回答

Adam Danz
Adam Danz 2022-3-15
编辑:Adam Danz 2022-3-15
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 个评论

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.
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!

请先登录,再进行评论。

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by