Asked by Matthias
on 18 Jan 2019

Hello,

a simple question: I've n-by-m matrices for X and Y. When I plot them, I get m differently colored lines connecting n points.

Example:

plot([1,1.1;2,2.1;3,3.1],[3,3.5;4,4.5;6,5],'-o')

With n=3 and m=2,

In the degenreated case with n=1, I expect and need to get m differently colored points. Instead the m points are connected to a single line.

Compare:

plot([1,1.1],[3,3.5],'-o')

How can I prevent this in this degenerated case?

Best regards

Answer by Stephen Cobeldick
on 18 Jan 2019

Edited by Stephen Cobeldick
on 18 Jan 2019

Accepted Answer

Good question. I also faced this issue a few years ago, and I found the simplest solution was to append NaN onto the bottom of the matrices, e.g.:

X(end+1,:) = NaN;

Y(end+1,:) = NaN;

plot(X,Y,'-o')

This forces MATLAB to treat each column as its own "line", even if it contains only one data point, while the NaNs are of course not plotted (so you do not need a special case, the NaNs can be appended to matrices of any size). Here is an example with your data:

>> X = [1,1.1];

>> Y = [3,3.5];

>> X(end+1,:) = NaN;

>> Y(end+1,:) = NaN;

>> plot(X,Y,'-o')

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Stephen Cobeldick (view profile)

Direct link to this comment:https://nl.mathworks.com/matlabcentral/answers/440362-plotting-n-by-m-matrix#comment_661753

## madhan ravi (view profile)

Direct link to this comment:https://nl.mathworks.com/matlabcentral/answers/440362-plotting-n-by-m-matrix#comment_661754

## Stephen Cobeldick (view profile)

Direct link to this comment:https://nl.mathworks.com/matlabcentral/answers/440362-plotting-n-by-m-matrix#comment_661762

## madhan ravi (view profile)

Direct link to this comment:https://nl.mathworks.com/matlabcentral/answers/440362-plotting-n-by-m-matrix#comment_661765

Sign in to comment.