How to use set() in a loop?

5 ビュー (過去 30 日間)
Johan
Johan 2012 年 5 月 3 日
Hi! I have lots of lots of images in a figure and I want to change them between being visible and invisible. My problem is that I don't know how to change severals of images in a loop. Now I have to type in set(_name1_,'Visible','off') for every image. I can't find a way to change the name in set(_name1_). What can I do?

回答 (4 件)

per isakson
per isakson 2012 年 5 月 3 日
Try something like this:
name_list = { 'name1', 'name2', 'name3', .... }; % row vector
for name = name_list
image_handle = findobj( 'PropertyName', name{1} ) % thanks to Daniel
set( image_handle, 'Visible', 'off')
end
The trick is to create name_list and that the names are unique
--- CONT. ---
Doc says:
himage = imshow(...) returns the handle to the image object created by imshow.
Thus, himage is a handle to an image object and 'Visible' is a property of a image object. Try
imh8 = imshow('Bar78.png',bar8);
set( imh8, 'Visible', 'off' )
I'm not sure this will do what you want, but ...
  5 件のコメント
Daniel Shub
Daniel Shub 2012 年 5 月 3 日
Somewhere Johan is going to do a loop and a variable naming scheme like imh8 and bar8 is going to probably cause him problems.
per isakson
per isakson 2012 年 5 月 3 日
@Daniel Obviously 'PropertyName' must match the PropertyValue, "name". Why do we care about Johans problem? I try give hints and point to the documentation, which I think is good. Sometimes coming up with a good search term is the real problem.

サインインしてコメントする。


Daniel Shub
Daniel Shub 2012 年 5 月 3 日
I believe your question is really: I created a bunch of variables called
name1
name2
name3
and now it is a nightmare to use them. This is also known as FAQ 4.6

Johan
Johan 2012 年 5 月 3 日
Thank you for fast answers! I tried to use cell arrays like this but get error messages
for i=1:8
Abar{i} = 1:i;
end
bar8 = axes('Units','Centimeters','Position',[6.98,1,0.5,1.11]);
imshow('Bar78.png',bar8);
Abar{8} = get(bar8,'children');
set(Abar{8}, 'Visible', 'off')
  2 件のコメント
Daniel Shub
Daniel Shub 2012 年 5 月 3 日
This should be an edit to your question and not an answer ...
per isakson
per isakson 2012 年 5 月 3 日
See my answer above

サインインしてコメントする。


Daniel Shub
Daniel Shub 2012 年 5 月 3 日
I am guessing a lot here, but what about something like
for ibar = 1:8
subplot(3, 3, ibar);
bar(ibar) = imshow(['Bar7', num2str(ibar), '.png');
end
set(bar, 'Visible', 'off')

カテゴリ

Help Center および File ExchangeWhite についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by