MATLAB Answers

Create output of rectangles with 200 different colours

조회 수: 2(최근 30일)
Faheem Malik
Faheem Malik 2021년 8월 18일
편집: DGM 2021년 8월 19일
Hi , I have been asked to create 200 different output of rectangles, all with different colours. Thank you in advacne for help


Adam Danz
Adam Danz 2021년 8월 18일
편집: Adam Danz 2021년 8월 18일
pos is a 1x4 vector defining [LeftSide, BottomSide, Width, Height].
See the FaceColor and EdgeColor name-values pairs to control color.
You could call it in a loop to produce multiple rectangles.
hold on
for i = 1:20
rectangle('position', rand(1,4).*randi(5,1,4), ...
'FaceColor', rand(1,3), 'EdgeColor','k')
axis tight
axis equal

DGM 2021년 8월 18일
편집: DGM 2021년 8월 19일
Adam already covered the sensible solution. I'll post my own.
MIMT randspots() generates an image containing random colored shapes:
% create a 800x1000 image containing 200 rectangles
% blob dimensions vary between 20-60 px
% blob opacity varies between 10-80%
A = randspots([800 1000 3],200,[20 60],[0.1 0.8],'rectangle');
I've been meaning to rewrite it, but I haven't bothered yet. The syntax is clumsy, but it works.
randspots() is from the MIMT on the File Exchange.
If neither of these solutions are what you're after, then you'll need to be more specific.
Ah. I guess if you're working from a color table, then randspots doesn't apply. As the name implies, all it can do is random.




Community Treasure Hunt

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

Start Hunting!

Translated by