Matlab GUI: Using a custom object tag for a button

15 Ansichten (letzte 30 Tage)
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 Kommentare
jojo jamila
jojo jamila am 18 Mai 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 am 18 Mai 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:

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Dominic Noel Kluck
Dominic Noel Kluck am 17 Mai 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 Kommentar
Image Analyst
Image Analyst am 17 Mai 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().

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 17 Mai 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

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Produkte


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by