Thank you Walter! At the end I decided to use quiver() to draw the discontinous lines. When updating the handlers you just update the "Plot.UData" with the first point of all lines and the "Plot.VData" with the second point of all lines. The only extra step is to set the 'ShowArrowHead' property to 'off' to get a line instead of an arrow. This was the most efficient, simple and clean method I found.
Updating Discountinuous Plot Lines Using Handlers
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Eddy Sanoli
am 22 Apr. 2020
Kommentiert: Eddy Sanoli
am 13 Mai 2020
Hey people!
Recently I needed to plot a bunch of discountinuous lines in a plot using a single call to the function. In this post ( https://la.mathworks.com/matlabcentral/answers/425844-draw-a-discontinuous-line ), its said that if you have two lines:
- Line 1: Starts in (x1, y1). Ends in (x2, y2).
- Line 2: Starts in (x3, y3). Ends in (x4, y4).
Then you can put create two arrays with the following structure
XCoords = [x1 x3; x2 x4];
YCoords = [y1 y3; y2 y4];
And then simply type
Lines = plot(XCoords, YCoords)
This solution worked great when I tried it on a static plot with 10 different lines, however, I need to dynamically update the XData and YData properties for an animation. The thing is, when I try to assign new values to the X and YData
Lines = plot(XCoords, YCoords) % This Works. XCoords / YCoords dims: 2 X 10 Matrix
Lines.XData = XCoords; % Error. PlotDataX dims: 2 X 10 Matrix
I get the following error: "Expected one output from a curly brace or dot indexing expression, but there were 10 results". I thought this came from a dimension mismatch so I printed the values from the plot XData and got the following output (.mlx file output).
ans = 1x2
x1 x2
ans = 1x2
x3 x4
...
ans = 1x2
x19 x20
It seemed like a dimension mismatch, so I tried the following fix
NewPlotDataX = XCoords(:)'; % NewPlotDataX dims: 1 X 20 Matrix
[Lines.XData] = NewPlotDataX;
Both vectors have exactly the same dimensions (1x20), but even then I get a new error: "Insufficient number of outputs from right hand side of equal sign to satisfy assignment". I don't know what to do anymore. Any fix for this? Maybe an alternate way to animate a bunch of discontinuous lines using a single plot command and handlers?
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 22 Apr. 2020
Lines = plot(XCoords, YCoords)
When either XCoords or YCoords (or both) are 2D (not vector), then some size matching will be done to make sure you have the best orientation for X relative to Y. If X is then 2D but Y is a vector, Y will be replicated to the same number of columns as X has. After that Y will be a 2D array, and the output, Lines, will contain one line() object for each column of Y.
Lines.XData = XCoords;
That is an error because Lines is more than one line() object. You cannot use dot notation to update more than one object.
In order to update more than one object in a single call, you need to use set(), and you have to be quite careful with the syntax of the set() call; I am having a difficult time getting it to work at the moment.
I advise you to loop setting corresponding coordinates, or use a hidden loop such as cellfun(). You will need to break it up per object (even if that just involves using num2cell()): you will definitely not be able to set the coordinates of all of the objects from a single numeric matrix.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Graphics Performance 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!