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

223 ビュー (過去 30 日間)
Ale Ale
Ale Ale 2016 年 4 月 10 日
編集済み: Image Analyst 2020 年 2 月 4 日
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 Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by