How can a change the line color in an axes with an already plotted figure (MATLAB GUIDE) ?

조회 수: 161 (최근 30일)
Hi, I would like to change the color of a plotted figure with a listbox. On each listbox you choose between the different colours. So the desired process is the following: Push a button --> Create a sine on axes. Select color in listbox --> Change color of sine. I want to know if there is a way to change the line colour without having to plot the sine again.
Thanks in Advance.

답변 (2개)

Chibuzo Nnonyelu
Chibuzo Nnonyelu 2020년 2월 4일
편집: Image Analyst 2020년 2월 4일
For cases where the plots are made by a function, for example using the pwelch, one is not able to place a variable for the plot handle. An alternative is demonstrated below.
L = 200;
fs = 1200;
f = 50;
t = (0:L-1)/fs;
x1 = 220 * sin(2*pi * f * t);
x2 = abs(x1);
figure
pwelch(x1, kaiser(L, 4), [], L, fs); hold on;
pwelch(x2, kaiser(L, 4), [], L, fs);
h = get(gca, 'Children');
set(h(1), 'Color', 'r');
0001 Screenshot.png

Image Analyst
Image Analyst 2016년 4월 10일
Get the handle to the curve you just drew, then later when you want to change the color, use the 'Color' property:
% Make a sine wave.
period = 2*pi;
amplitude = 10;
x = linspace(-4*pi, 4*pi, 500);
y = amplitude * sin(2 * pi * x / period);
% First plot in blue. Be sure to capture the handle of the curve you just drew.
hCurve = plot(x, y, 'b-', 'LineWidth', 5);
grid on;
uiwait(msgbox('Click OK to change the color'));
% Now change the color to red.
hCurve.Color = 'r';
% ALternatively, with OLDER VERSIONS of MATLAB.
% set(hCurve, 'Color', 'r');
  댓글 수: 2
Ale Ale
Ale Ale 2016년 4월 10일
Thanks for your answer. So could I use the hCurve handle in the Listbox Callback?
Image Analyst
Image Analyst 2016년 4월 10일
One way to get the handles to all lines in an axes is to use findobj():
axesHandlesToAllLines = findobj(h, 'Type', 'line');
If you have only one line, you're all set. If you have multiple lines, you're going to have to figure out which one you want to change. This can involve saving the handles to all the lines in a global variable. See the FAQ for how to do this: http://matlab.wikia.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F

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

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by