Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You've got a couple of options, depending on your circumstances. You can either sort the data to prevent the discontinuities, or you can insert a few <code>NaN</code>s into the X and Y vectors.</p> <ol> <li><p>To sort:</p> <pre><code>[xSorted, ixsSort] = sort(x); ySorted = y(ixSort); plot(xSorted, ySorted); </code></pre></li> <li><p>To add nans, you need to do some additional work to determine where the breaks should be, and then insert NaNs. For example, to break the line after the 10th entry</p> <pre><code>xBroken = [x(1:10) nan x(11:end)]; yBroken = [y(1:10) nan y(11:end)]; plot(xBroken, yBroken); </code></pre> <p>Edit: see sample code below for a more comprehensive example.</p></li> <li><p>Of course, as an easy backup, just do a scatter plot instead of a line plot:</p> <pre><code>plot(x, y, '.'); </code></pre></li> </ol> <hr> <p>Some sample code for method 2 below:</p> <pre><code>%Some sample data x = [1:10 2.1:11 3.2:12]; y = randn(size(x)); %Define where breaks are needed (and associated boundaries) ixsBreaksNeeded = find(diff(x)&lt;0); ixsSegmentBoundaries = [0 ixsBreaksNeeded length(x)]; %This makes the iterations a lot easier %Predefine some nan vectors to move data into xBroken = nan(1, length(x) + length(ixsBreaksNeeded)); yBroken = nan(1, length(x) + length(ixsBreaksNeeded)); %Move data segments into nan vectors, leaving gaps between segments ixOffset = 0; for ix = 2:length(ixsSegmentBoundaries) ixsOriginal = (ixsSegmentBoundaries(ix-1)+1):ixsSegmentBoundaries(ix); xBroken(ixsOriginal + (ixOffset)) = x(ixsOriginal); yBroken(ixsOriginal + (ixOffset)) = y(ixsOriginal); ixOffset = ixOffset+1; end %Plot to demonstrate subplot(211) plot(x,y); subplot(212) plot(xBroken, yBroken) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload