Question

I have a matrix M of data and a matrix E of errors that I want to plot with errorbar against an independent variable x. I would also like lines between points in the same row. One column in M or E corresponds to one x-value, so this can be accomplished with

errorbar(x, M, E, 'o-')

I am missing some data points and these have the value NaN. As it should errorbar() ignores these, but the line between the points closest two points in the same row (the one resulting from the '-' option) is broken.

I have seen some different solutions that work with vectors as opposed to matrices, involving ~any(isnan(y),1) and L=~(isnan(x)|isnan(y)), but these lead to problems like

Error using errorbar
X, Y and error bars must all be the same length

I can't find a solution for matrices, any ideas?

Was it helpful?

Solution

Because the missing data points are probably not the same for every line I advice to plot in a loop like this (assuming plot data are columns of the matrices):

for i = 1 : size(M, 2)
  data = M(:, i);
  good = not(isnan(data));
  xi = x(good);
  data = data(good);
  error = E(good, i);
  errorbar(xi, data, error, 'o-');
  hold on;
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top