Hiding line visibility with state button

57 views (last 30 days)
Lisa Hildebrand
Lisa Hildebrand on 23 Mar 2020
Commented: Rik on 24 Mar 2020
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!

Answers (1)

darova
darova on 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 Comments
darova
darova on 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 on 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.

Sign in to comment.

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!