![0001 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/269960/0001%20Screenshot.png)
How can a change the line color in an axes with an already plotted figure (MATLAB GUIDE) ?
139 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Chibuzo Nnonyelu
am 4 Feb. 2020
Bearbeitet: Image Analyst
am 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');
![0001 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/269960/0001%20Screenshot.png)
0 Kommentare
Image Analyst
am 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 Kommentare
Image Analyst
am 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
Siehe auch
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!