How can I plot a bode diagram with random color?

What should I add to this code to have a random color?
sys=tf(1,[1 1])
bode(sys)

1 Kommentar

Paulo Silva
Paulo Silva am 21 Jun. 2011
Please always provide detailed questions so we can help you better and faster

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Paulo Silva
Paulo Silva am 21 Jun. 2011

2 Stimmen

Another strange question from sadel, I never seen someone doing so many strange things but it's all good, here's a variation from Chirag Gupta code:
sys=tf(1,[1 1]);
ColList='ymcrgbk'; %no w letter because you won't see the white line
col=randi([1 7]);
bode(sys,ColList(col))

10 Kommentare

sadel
sadel am 21 Jun. 2011
:) in another part of my code, I use the plot function this way:
plot(t,y,'DisplayName',string,'Color',[rand;rand;rand])
and this produces random colors. I'm looking for something similar with this for bode. Is it possible?
Paulo Silva
Paulo Silva am 21 Jun. 2011
AllLines=findall(gcf,'type','line')
set(AllLines,'color',[rand,rand,rand])
sadel
sadel am 21 Jun. 2011
Nope, let's say that I have a button and when I click it, this produces a bode diagram. I use the "hold" function to hold multiple diagrams on the same axes. I want every new diagram having a random color. I think that your code will change the color of all the lines everytime that I will click the button. I don't want that. I'm think to have dynamicLegend. Is this possible?
Paulo Silva
Paulo Silva am 21 Jun. 2011
It's funny how you only give enough details after we spent time guessing what you really want!
Forget the random color and do thing the best and simplest way possible, just use hold all like this
clf
sys=tf(1,[1 1])
sys1=tf(1,[1 1 1])
hold all
bode(sys)
bode(sys1)
sadel, to answer your question, bode() and bodeplot() do NOT accept numeric colors. bode() also has the property that it does not return the handle to the plots it has created. Thus my answer below that uses bodeplot() which does return the handles.
sadel
sadel am 21 Jun. 2011
I'm sorry Paulo but this my first program with Matlab and I don't know all the functions and their abilities yet. And my english is very bad. It's not easy to say what I want.
@Walter I think that the "hold all" function is the best that I can use.
Paulo Silva
Paulo Silva am 21 Jun. 2011
just a simple example code
function testbode
figure
pbh = uicontrol(gcf,'Style','pushbutton','String','Add another',...
'Position',[10 20 60 40],...
'callback',@newbode);
axes
hold all
function newbode(obj,event)
num=ones(1,randi([1,2]));
den=ones(1,randi([3,6]));
sys=tf(num,den)
hold all
bode(sys)
end
end
Paulo Silva
Paulo Silva am 21 Jun. 2011
no problem sadel, you do your best and appreciate all the help people give by voting and accepting answers, that's very important because others might have the same questions and will surely read these questions and answers.
sadel
sadel am 21 Jun. 2011
Can I use Dynamic Legend with "hold all"?
Paulo Silva
Paulo Silva am 21 Jun. 2011
function testbode
figure
pbh = uicontrol(gcf,'Style','pushbutton','String','Add another',...
'Position',[10 20 60 40],...
'callback',@newbode);
axes
hold all
function newbode(obj,event)
num=ones(1,randi([1,2]));
den=ones(1,randi([3,6]));
sys=tf(num,den)
hold all
sysn=evalc('sys')
[a b] = strread(sysn, '%s %s', 'delimiter',char(10))
num=char(a(2));
den=char(a(3));
linesep=char(b(2));
set(sys,'Name',[num char(10) linesep char(10) den])
bode(sys)
legend('-DynamicLegend');
end
end

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (4)

Chirag Gupta
Chirag Gupta am 21 Jun. 2011

1 Stimme

colors =['r','b','g','y','m','k'];
c = ceil(6*(rand))
bode(sys,colors(c))
This will pseudo randomly choose one of the 6 colors. You can obviously add more colors to the list!

1 Kommentar

Chirag Gupta
Chirag Gupta am 21 Jun. 2011
MATLAB figures default to a particular color. Unless you change that setting or change the colors via code, you will get the same color.

Melden Sie sich an, um zu kommentieren.

Walter Roberson
Walter Roberson am 21 Jun. 2011

1 Stimme

Provided that your systems are not MIMO,
sys=tf(1,[1 1]);
h = bodeplot(sys); %notice this is _not_ bode()
for L = findobj(h,'type','line')
set(L,'Color', rand(1,3));
end
Note: this does not attempt to match colors between the frequency and phase components. You did ask for random color...
I do not have the appropriate toolbox to probe to determine what would be needed to use the same color between the two halves when multiple systems are being drawn on the same plot, or when bode() is used to draw an array of bode plots for a MIMO system.

4 Kommentare

sys=tf(1,[1 1]);
h = bodeplot(sys); %notice this is _not_ bode()
set(findobj(h,'type','line'),'Color', rand(1,3));
Not valid for MIMO systems or for specifying multiple systems to be plotted simultaneously.
Paulo Silva
Paulo Silva am 21 Jun. 2011
Walter your code allows similar colors on the same axes, would be better to use hold all or make a random list of colors so each new bode plot would use just one color from the list (until the last color and it returns to the first of the list)
If you cannot have similar or duplicate colors, then the colors have not been chosen at random as requires by the original question ;-)
Paulo Silva
Paulo Silva am 21 Jun. 2011
Good point Walter :)

Melden Sie sich an, um zu kommentieren.

Niels
Niels am 2 Mai 2013

1 Stimme

Here is a solution for giving bode plots a different color than and then 'ymcrgbk' and subsequently saving the figure without unwanted changes to the legend.
sys1=tf(1,[1 1]); sys2=tf(1,[1 1 1]);
figure; hold on;
bode(sys1,'b'); bode(sys2,'r');
h1 = findobj(gcf,'Color','b','-and','linestyle','-'); set(h1,'linewidth',1,'color',[.9 .9 .9]);
h2 = findobj(gcf,'Color','r','-and','linestyle','-'); set(h2,'linewidth',2,'color',[.5 .5 .5]);
legend([h1(1) h2(1)],'system 1','system 2')
It is important to assign the first term of the handle (e.g. h1(1)) in order to avoid issues with matrix dimentions (horzcat)
Chirag Gupta
Chirag Gupta am 21 Jun. 2011

0 Stimmen

bode(sys,'r')
You can always write some code to pick a random color from a set of colors that you would like

1 Kommentar

sadel
sadel am 21 Jun. 2011
No, this is not what I want. I want the program to choose a color. Not me. This code produces bode with red color.

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by