Hiding line visibility with state button
49 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
darova
2020-3-23
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 个评论
darova
2020-3-24
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
2020-3-24
@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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!