Effacer les filtres
Effacer les filtres

AI Chat Playground generated an interactive plot with a slider and a bug

7 vues (au cours des 30 derniers jours)
Tomazz
Tomazz le 12 Avr 2024
Commenté : Tomazz le 15 Avr 2024
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

Réponse acceptée

Ayush Modi
Ayush Modi le 12 Avr 2024
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 commentaire
Tomazz
Tomazz le 12 Avr 2024
Hi Ayus,
if I wrote the function, I would use plot function, too.
It seems this is a wierd behavior of line function.
Thanks,
Tomazz

Connectez-vous pour commenter.

Plus de réponses (1)

DGM
DGM le 12 Avr 2024
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 commentaires
DGM
DGM le 12 Avr 2024
Modifié(e) : DGM le 12 Avr 2024
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.
Tomazz
Tomazz le 15 Avr 2024
Cool, all clear now, thanks!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Polar Plots dans Help Center et File Exchange

Produits


Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by