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 件のコメント

Jan
Jan 2022 年 3 月 13 日
Is the GUI a figure or uifigure? Do you use AppDesigner, GUIDE or create the GUI programmatically? What does "pass to ECU" mean?
Rik
Rik 2022 年 3 月 14 日
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
KUBER DWIVEDI
KUBER DWIVEDI 2022 年 3 月 15 日
編集済み: KUBER DWIVEDI 2022 年 3 月 15 日
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.
Rik
Rik 2022 年 3 月 15 日
How would you do this outside of a GUI?
KUBER DWIVEDI
KUBER DWIVEDI 2022 年 3 月 15 日
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.
KUBER DWIVEDI
KUBER DWIVEDI 2022 年 3 月 15 日
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.
KUBER DWIVEDI
KUBER DWIVEDI 2022 年 3 月 15 日
And I in my case GUI is uifigure.
Rik
Rik 2022 年 3 月 15 日
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 2022 年 3 月 16 日
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 日

0 投票

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 件のコメント

KUBER DWIVEDI
KUBER DWIVEDI 2022 年 3 月 16 日
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 2022 年 3 月 16 日
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 ExchangeDevelop Apps Using App Designer についてさらに検索

質問済み:

2022 年 3 月 13 日

コメント済み:

2022 年 3 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by