MATLAB Answers

Create output of rectangles with 200 different colours

3 views (last 30 days)
Faheem Malik
Faheem Malik on 18 Aug 2021
Edited: DGM on 19 Aug 2021
Hi , I have been asked to create 200 different output of rectangles, all with different colours. Thank you in advacne for help

Answers (2)

Adam Danz
Adam Danz on 18 Aug 2021
Edited: Adam Danz on 18 Aug 2021
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 on 18 Aug 2021
Edited: DGM on 19 Aug 2021
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