Matlab GUI: Using a custom object tag for a button

9 vues (au cours des 30 derniers jours)
Dominic Noel Kluck
Dominic Noel Kluck le 17 Mai 2023
Commenté : Image Analyst le 18 Mai 2023
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 commentaires
jojo jamila
jojo jamila le 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 le 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:

Connectez-vous pour commenter.

Réponse acceptée

Dominic Noel Kluck
Dominic Noel Kluck le 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 commentaire
Image Analyst
Image Analyst le 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().

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 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

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Produits


Version

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by