How can a change the line color in an axes with an already plotted figure (MATLAB GUIDE) ?
164 ビュー (過去 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.
0 件のコメント
回答 (2 件)
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');
0 件のコメント
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 件のコメント
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 Exchange で Migrate GUIDE Apps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!