Hiding line visibility with state button

Hello,
I am trying to make it possibly to turn on and off the visibility of lines using a state button, and I am finding that my background knowledge of functions is very lacking.
Here is what I have so far from looking at the examples:
function mycustomstatebutton
fig=uifigure;
ax=uiaxes('Parent',fig,'position',[104 123 300 201]);
btn=uibutton(fig,'state','Value',true,'position',[420 218 100 22]);
btn.ValueChangedFcn=@customcallback;
function customcallback(src,event)
T=[1 2 3 4 5 6 7 8];
X=[1 3 5 2 6 7 3 1];
Y=[8 7 6 4 2 9 3 1];
pp=plot(ax,T,Y)
switch src.Value
case 'off'
pp.Visible='off'
case 'on'
pp.Visible='on'
end
end
end
This creates a plot that can be turned on when the button is clicked, but it does not turn off. I am confused about a few things:
-I have not been able to find what src actually is/what it means. (I'm assuming from context that it's the button's value based on the user's input?)
-why doesn't this work? Do I need to do something to make it refresh and realize that the button is being turned on and off, or is there something more fundamentally wrong with what I'm doing?
Any suggestions would be appreciated!

Réponses (1)

darova
darova le 23 Mar 2020
Try this
function mycustomstatebutton
fig=figure;
% ax=uiaxes('Parent',fig,'position',[104 123 300 201]);
T=[1 2 3 4 5 6 7 8];
X=[1 3 5 2 6 7 3 1];
Y=[8 7 6 4 2 9 3 1];
ax = gca;
plot(ax,T,Y);
set(ax,'buttondownfcn',@customcallback) % set callback to current axes
function customcallback(src,~)
h = get(src,'children'); % get curve using object handler
if strcmp(get(h,'visible'),'on')
set(h,'visible','off')
else
set(h,'visible','on')
end
end
end

4 commentaires

Lisa Hildebrand
Lisa Hildebrand le 24 Mar 2020
Modifié(e) : Lisa Hildebrand le 24 Mar 2020
Thank you! That definitely answers my question as I asked it, and I realize I should probably have asked it a bit differetly. Is there a way to turn on/off individual lines based on buttons being clicked? If I understand your code correctly, it collects all "children" (which I believe are the lines on the plot) and turns them on or off when the mouse is clicked. I guess my questions are:
-Is there some way to collect a single "child" at a time?
-Can "buttondownfcn" be applied to a button's state, or does it only work with the mouse?
Answer I found: no, it is not possible to use "buttondownfcn" with a button.
I realize that there are other people who have asked questions about similar things, but I didn't understand what they were doing well enough to be able to implement the answers. If anyone has time to explain to me why my original code doesn't work, I was thinking that might help give me an idea of what I should be looking at next.
I'll keep looking for answers and edit this post if I find them. Thank you for your help!
Rik
Rik le 24 Mar 2020
You should store the handles to the objects you want to be hidden in an array. That way you don't need the brute-force get(scr,'Children').
darova
darova le 24 Mar 2020
Another approach using questdlg. See script inside
  • That way you don't need the brute-force get(scr,'Children').
Why is it brute?
  • If anyone has time to explain to me why my original code doesn't work
Some tips:
Each time you click you draw a curve
It doesn't test if line visible or not
src variable is a handler on the object you clicked. I don't know if it has src.value property
Rik
Rik le 24 Mar 2020
@Darova, I called it brute-force, because it gets all the children, not just the ones you need. In this case there isn't a difference, but it is not hard to image a future reader wanting to hide only some objects stumbling across this thread. In such a case the suggestion of storing the handles when the objects are created makes sense. I would argue storing handles of objects you create in a GUI very nearly always makes sense.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Commenté :

Rik
le 24 Mar 2020

Community Treasure Hunt

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

Start Hunting!

Translated by