Call back function for state button in matlab gui
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
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.
採用された回答
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
- 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 件のコメント
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 件)
参考
カテゴリ
Help Center および 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!