필터 지우기
필터 지우기

How can I plot a bode diagram with random color?

조회 수: 25 (최근 30일)
sadel
sadel 2011년 6월 21일
What should I add to this code to have a random color?
sys=tf(1,[1 1])
bode(sys)
  댓글 수: 1
Paulo Silva
Paulo Silva 2011년 6월 21일
Please always provide detailed questions so we can help you better and faster

댓글을 달려면 로그인하십시오.

채택된 답변

Paulo Silva
Paulo Silva 2011년 6월 21일
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
sadel
sadel 2011년 6월 21일
Can I use Dynamic Legend with "hold all"?
Paulo Silva
Paulo Silva 2011년 6월 21일
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

댓글을 달려면 로그인하십시오.

추가 답변 (4개)

Chirag Gupta
Chirag Gupta 2011년 6월 21일
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
Chirag Gupta
Chirag Gupta 2011년 6월 21일
MATLAB figures default to a particular color. Unless you change that setting or change the colors via code, you will get the same color.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2011년 6월 21일
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
Walter Roberson
Walter Roberson 2011년 6월 21일
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 2011년 6월 21일
Good point Walter :)

댓글을 달려면 로그인하십시오.


Niels
Niels 2013년 5월 2일
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 2011년 6월 21일
bode(sys,'r')
You can always write some code to pick a random color from a set of colors that you would like
  댓글 수: 1
sadel
sadel 2011년 6월 21일
No, this is not what I want. I want the program to choose a color. Not me. This code produces bode with red color.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Response Computation and Visualization에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by