Matlab GUI: Using a custom object tag for a button

13 views (last 30 days)
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 Comments
jojo jamila
jojo jamila on 18 May 2023
Hello, I need help in MATLAB, please, on how to write a process in the button in the guide program
Image Analyst
Image Analyst on 18 May 2023
@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:

Sign in to comment.

Accepted Answer

Dominic Noel Kluck
Dominic Noel Kluck on 17 May 2023
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 Comment
Image Analyst
Image Analyst on 17 May 2023
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().

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 17 May 2023
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

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2017a

Community Treasure Hunt

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

Start Hunting!