AI Chat Playground generated an interactive plot with a slider and a bug
조회 수: 21 (최근 30일)
이전 댓글 표시
I asked MATLAB AI Chat Playground to generate an interactive plot with a slider.
With slight modification, the code runs (in desktop environment) , but the plot is updated only when the slider value
is increased, not when decreased. Can anyone spot why?
% Create the UI figure
fig = uifigure('Name', 'Interactive Sin Plot');
fig.Position(3:4) = [400 300];
% Create the plot
ax = uiaxes(fig);
ax.Position = [50 50 300 150];
x = linspace(0, 2*pi, 1000);
y = sin(x);
line(ax, x, y, 'Color', 'b', 'LineWidth', 2);
% Create the slider
slider = uislider(fig);
slider.Position = [50 200 300 3];
slider.Limits = [0.1 10];
slider.Value = 1;
slider.MajorTicks = [0.1 1 10];
slider.ValueChangedFcn = @(sld, event) updatePlot(sld.Value, ax);
% Function to update the plot based on the slider value
function updatePlot(period, ax)
x = linspace(0, 2*pi*period, 1000);
y = sin(x);
line(ax, x, y, 'Color', 'b', 'LineWidth', 2);
end
댓글 수: 0
채택된 답변
Ayush Modi
2024년 4월 12일
Hi Tomazz,
This seems like a bug. As a workaround, use plot function instead of line function. This updated the plot even when the slider value is decreased.
plot(ax, x, y, 'Color', 'b', 'LineWidth', 2);
Please refer to the following MathWorks documentation for more information on the plot function:
As for why line function is not working, you can report it to the Technical Support team by creating a Service Request: https://mathworks.com/support/contact_us.html
추가 답변 (1개)
DGM
2024년 4월 12일
Try just updating the existing line object instead of creating new line objects.
% Create the UI figure
fig = uifigure('Name', 'Interactive Sin Plot');
fig.Position(3:4) = [400 300];
% Create the plot
ax = uiaxes(fig);
ax.Position = [50 50 300 150];
x = linspace(0, 2*pi, 1000);
y = sin(x);
hl = line(ax, x, y, 'Color', 'b', 'LineWidth', 2);
% Create the slider
slider = uislider(fig);
slider.Position = [50 200 300 3];
slider.Limits = [0.1 10];
slider.Value = 1;
slider.MajorTicks = [0.1 1 10];
slider.ValueChangedFcn = @(sld, event) updatePlot(sld.Value, hl);
% Function to update the plot based on the slider value
function updatePlot(period, hl)
x = linspace(0, 2*pi*period, 1000);
y = sin(x);
hl.XData = x;
hl.YData = y;
end
댓글 수: 3
DGM
2024년 4월 12일
편집: DGM
2024년 4월 12일
The reason is because the axes extents are set implicitly by the range of the data plotted by all the line objects, and the old line objects don't get deleted. As a consequence, the axes limits only change if you create a set of XData which spans a larger interval than any prior plot. The new "invisible" lines are there, they're just coincident with the existing lines over a shorter interval.
You can see the behavior in action if you use a different line color for each plot.
line(ax, x, y,'color',rand(1,3),'LineWidth', 2);
FWIW, chart.line primitives created by plot() have a few different interactions with their parent axes upon creation when compared to graphics.line primitives created by line(). They're similar, but just bear in mind they're not really expected to be the same.
참고 항목
카테고리
Help Center 및 File Exchange에서 Graphics Object Programming에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!