Changing the same property of some axis children using subsasgn

How to make the following code work in order to change the 'Visible' property of some children plots whose handles are stored in toHide using subsasgn?
f1 = figure;
ax1=axes(f1);
axis(ax1,'tight');
ax1.NextPlot = 'add';
plot(ax1,[1,2],[3,4],'DisplayName','graph 1')
plot(ax1,[1,2],1.5*[3,4],'DisplayName','graph 2')
plot(ax1,[1,2],2*[3,4],'DisplayName','three')
toHide = findobj(ax1,'-regexp','DisplayName','graph');
%% Method 1
toHide(:).Visible = {'off','off'};
%% Method 2
S = substruct('()',{':'},'.','Visible');
toHide(:).Visible = subsasgn( toHide , S , {'off','off'} );

4 Comments

you can easily hide the objects in toHide by using
set(toHide,'Visible','off')
is there any reason you want to use subsasgn() ?
I need to learn more about its usage. I understand there are many ways to do what I need, but I am eager to gain more knowledge about it.
you could use
for nr=1:numel(toHide)
S(1).type='()';
S(1).subs={nr};
S(2).type = '.';
S(2).subs = 'Visible';
toHide = subsasgn( toHide , S , 'off' );
end
if the objects could be indexed directly this would be more easy, but your Method 1 does not work in my matlab

Sign in to comment.

 Accepted Answer

I assume you know that subsasgn is called implicitly whenever you make an assignment statement, as in the following:
[toHide.Visible]=deal('off')

4 Comments

Your answer solved for me another issue I couldn't understand. When writing your own answer without brackets as in
toHide.Visible = deal('off')
I get an error: Assigning to 2 elements using a simple assignment statement is not supported. Consider using comma-separated list assignment, which confused me a lot.
Last question: is it possible to fix my line of using subsasgn in the main question to make it work?
Last question: is it possible to fix my line of using subsasgn in the main question to make it work?
I thought Jonas did fix it for you.
I thought it could be done without the need to use for-loop.
No, it cannot be done solely with subsasgn, with no loops.

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Asked:

on 18 Apr 2021

Commented:

on 18 Apr 2021

Community Treasure Hunt

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

Start Hunting!