Filter löschen
Filter löschen

Randomizing an array of colors

28 Ansichten (letzte 30 Tage)
Hao Wei Low
Hao Wei Low am 29 Apr. 2015
Bearbeitet: Søren Jensen am 29 Apr. 2015
I'm trying to randomize an array of colors that can be used on a pushbutton and i can't seem to get them to randomize or work at all. Below is the code i thought my work.
colors={'b' 'r' 'g' 'y'}
rand=randperm(numel(colors))
randcolors=colors(rand)
set(handles.pushbutton1,'background',randcolors{1})
  1 Kommentar
Guillaume
Guillaume am 29 Apr. 2015
I don't know anything about gui to help you but I wouldn't use rand as a variable name since that shadows matlab's rand function.
Otherwise the randomisation code looks fine, so that's not the issue.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Søren Jensen
Søren Jensen am 29 Apr. 2015
Bearbeitet: Søren Jensen am 29 Apr. 2015
I thought i remembered something about specifying colors by numbers.. here it is: http://se.mathworks.com/help/matlab/ref/colorspec.html
just feed it with three random numbers between 0 and 1 (rand), or random rounded numbers for only 0 and 1.
ex:
randcolor = [round(rand) round(rand) round(rand)]
EDIT: just did a little playing around with the plot function.. here's an example of generating random plot colors every time:
plot(your_plot,'color',[rand rand rand])

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by