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

275 views (last 30 days)
Ale Ale on 10 Apr 2016
Edited: Image Analyst on 4 Feb 2020
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.
0 CommentsShow -1 older commentsHide -1 older comments

Sign in to comment.

Answers (2)

Chibuzo Nnonyelu on 4 Feb 2020
Edited: Image Analyst on 4 Feb 2020
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');
0 CommentsShow -1 older commentsHide -1 older comments

Sign in to comment.

Image Analyst on 10 Apr 2016
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 CommentsShow 1 older commentHide 1 older comment
Image Analyst on 10 Apr 2016
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

Sign in to comment.

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by