Hey Rauf,
You're on the right track—you can't draw a line directly on a uifigure, but you can draw it on UIAxes. The key here is to update the line every time your points change instead of plotting a new one on top of the old one.
Here’s a simple way to do it:
- Use plot to create the initial line in your UIAxes.
- Update the XData and YData of the line object instead of re-plotting.
Example Code (App Designer)
app.LineHandle = plot(app.UIAxes, [app.P1(1), app.P2(1)], [app.P1(2), app.P2(2)], 'r-', 'LineWidth', 2);
app.P1 = app.P1 + rand(1,2) * 0.1;
app.P2 = app.P2 + rand(1,2) * 0.1;
app.LineHandle.XData = [app.P1(1), app.P2(1)];
app.LineHandle.YData = [app.P1(2), app.P2(2)];
How This Works
- startupFcn initializes the plot once and stores the line handle.
- UpdateLine updates the XData and YData properties without redrawing the entire figure.
- No flickering, no unnecessary new lines stacking up!
If you’re triggering updates on button clicks or real-time changes, just call UpdateLine(app). Should work smoothly! 🚀
If this helps, follow me so you can message me anytime with future MATLAB or Simulink questions! 🚀