Matlab GUI: Using a custom object tag for a button

13 次查看(过去 30 天)
I got a problem with my GUI where I have multiple buttons which should change there color between red and green when you press it.
Im trying to create an external fuction which at first should get the object tag and put it into the variable "temp". After that there is a problem with retrieving the object color. If I press the button it says theres no object named "temp". Here is some example code:
temp = hObject.Tag;
color = get(handles.temp,'BackgroundColor');
Is there any way of getting Matlab to read the variable and put it instead of "temp"?
Thanks in advance
  2 个评论
jojo jamila
jojo jamila 2023-5-18
Hello, I need help in MATLAB, please, on how to write a process in the button in the guide program
Image Analyst
Image Analyst 2023-5-18
@jojo jamila you can use the text editor built-in to MATLAB.
For a tutorial that has most kinds of widgets you will use, you can try MAGIC:
Also look at App Designer which is how they prefer you do it now, though it does have disadvantages over GUIDE (no debugging, workspace and command window not included, etc.) as well as advantages (more OOP, better widgets).
If you have any more questions, then attach your data and code to read it in with the paperclip icon after you read this:

请先登录,再进行评论。

采纳的回答

Dominic Noel Kluck
Dominic Noel Kluck 2023-5-17
I answered it myself, hope someone can use it. I just needed to put the "temp" in brackets.
temp = hObject.Tag;
color = get(handles.(temp),'BackgroundColor');
^ thats it
  1 个评论
Image Analyst
Image Analyst 2023-5-17
That's very cryptic code. And your code doesn't even check or change the colors.
I suggest you do it my way instead, using modern OOP coding. It's much easier to read and understand. So much more intuitive than using dynamic field names in parentheses and using set() and get().

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2023-5-17
Not sure what you mean by "read the variable". You know the callback function name so just use the known name of the button. For example, if the button is called/tagged "btnGo", then in its callback function do this:
function btnGo_Callback(hObject, eventdata, handles)
backgroundColor = handles.btnGo.BackgroundColor;
% Toggle color between red and green.
if isequal(backgroundColor, [1,0,0])
% It's red so change it to green.
handles.btnGo.BackgroundColor = [0,1,0];
else
% It's green (or something else) so change it to red.
handles.btnGo.BackgroundColor = [1,0,0];
end
end

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by