0

I want to plot a dark green and a dark green dashed graph, but unfortunately MatLab complains that the vector does not have the same length

Error using plot

Vectors must be the same length.

Error in EasySim (line 174) `

plot(x,z1,'b--',x,z2,'c--',x,z3,'b',x,z4,'c',x,z5,'g',x,z6,'g--',x,z7,'color',[0 0.5 0],x,z8,'color',[0 0.5 0],'linestyle','--')
Community
  • 1
  • 1
KCordes
  • 1
  • 1
  • 1
  • You should add the matlab tag, and add some example data to make a reproducible example – alan ocallaghan Mar 01 '18 at 15:01
  • Read what a [mcve] is, and include one. Note that it must be **minimal**, so no need of 8 variables, and **complete**, we should able to copy paste it and run it – Ander Biguri Mar 01 '18 at 15:51
  • But my Question is why [0 0.5 0] doesnt fit? – KCordes Mar 01 '18 at 15:58
  • The problem is probably that one of your vectors you're trying to plot has a different length. This doesn't have anything to do with the colours. – ViG Mar 01 '18 at 17:03
  • But if i choose colours like 'b--' etc. my code works. Also the error is for the line of the plot – KCordes Mar 01 '18 at 17:16

2 Answers2

1

You cannot have more then one 'color', [R G B] in one plot statement.

so your code is like you write:

plot(x,z1,'b--',x,z2,'c--',x,z3,'b',x,z4,'c',x,z5,'g',x,z6,'g--',x,z7,'color',[0 0.5 0],x,z8,[0 0.5 0],'linestyle','--')

so the last line is x,z8,[0 0.5 0]. because x and z8 is not in length of 3 you got this error.

Note: if x and z8 is 3 elements each you got different error: Data must be a single matrix Y or a list of pairs X,Y.

you can check it on this example:

x=1:5
z1=x.^2;
z2=x.^3;
z3=x.^4;
z4=x.^5;
z5=x.^6;
z6=x.^7;
z7=x.^8;
z8=x.^9;

change x to x=1:3 to see what happen in this case

yona bendelac
  • 114
  • 2
  • 10
1

As per this answer:

You cannot have more then one 'color', [R G B] in one plot statement.

You can change the colors after you've plotted them:

x = 0:4;
h = plot(x, x, '-', x, x.^2, '--');
set(h(1), 'color', [1 0 1])
set(h(2), 'color', [0 0.5 0])
Steve
  • 1,579
  • 10
  • 23