Matlab GUI: Using a custom object tag for a button

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

Hello, I need help in MATLAB, please, on how to write a process in the button in the guide program
@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일

0 개 추천

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

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

카테고리

도움말 센터File Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품

릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by