Fixing Matlab Plot Error: Invalid Color/Linetype Argument

  • Context: MATLAB 
  • Thread starter Thread starter MathematicalPhysicist
  • Start date Start date
  • Tags Tags
    Plot
Click For Summary

Discussion Overview

The discussion revolves around an error encountered in Matlab when attempting to plot a sine function. Participants explore the cause of the error related to the color and line type argument in the plot command, examining the implications of character encoding and formatting in code snippets.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports an error message when using 'k−' in the plot command, seeking clarification on the issue.
  • Another participant suggests checking the Matlab plot command documentation for examples of valid arguments.
  • Several participants propose that the error lies in the use of 'k−', with some suggesting it should be '-k', which resolves the issue for them.
  • There is a discussion about whether the order of symbols matters, with some participants noting that the documentation states it does not.
  • One participant asserts that the problem is due to a non-standard dash character being used instead of a regular minus sign, which causes the error.
  • Another participant shares their experience with character encoding issues when copying code from other sources.

Areas of Agreement / Disagreement

Participants generally agree that the error is related to the character used in the plot command, but there is some disagreement about whether it is a bug or a result of character encoding issues. The discussion remains unresolved regarding the implications of the documentation's claims about symbol order.

Contextual Notes

Participants mention different operating systems and versions of Matlab, which may influence their experiences with the error. There is also a reference to the potential pitfalls of copying and pasting code from external sources, highlighting the need for careful text handling.

MathematicalPhysicist
Science Advisor
Gold Member
Messages
4,662
Reaction score
372
I am using Matlab's 2017Rb academic license.

And while trying to operate the following sequence of commands:
Matlab:
x = linspace(0,2*pi,1000);
y = sin(2*x);
plot(x,y,'k−','linew',2); hold on;

I get the following warning:
Matlab:
Error using plot
Error in color/linetype argument.

What seems to be wrong in the above code?

Thanks.
 
Physics news on Phys.org
So the error is in 'k-', how it should be?
'-k', yes now it works.
 
  • Like
Likes   Reactions: AgusCF and jedishrfu
MathematicalPhysicist said:
So the error is in 'k-', how it should be?
'-k', yes now it works.
But... '-k' make the same as 'k' I think...
 
MathematicalPhysicist said:
So the error is in 'k-', how it should be?
'-k', yes now it works.
Interesting. The documentation says that the order of the symbols doesn't matter. So this is either a bug or there is something else going on.
 
  • Like
Likes   Reactions: AgusCF
FactChecker said:
Interesting. The documentation says that the order of the symbols doesn't matter. So this is either a bug or there is something else going on.
Did you try it in your machine?

I have Ubuntu 16.04 as the OS in this computer.
 
AgusCF said:
But... '-k' make the same as 'k' I think...
Yes, it works as well.
 
  • Like
Likes   Reactions: AgusCF
MathematicalPhysicist said:
Yes, it works as well.
And... Why were you trying to plot 'k-'?
 
AgusCF said:
And... Why were you trying to plot 'k-'?
I copy-paste this snippet of code from some lecture notes I am reading.

So there I had this 'k-'.
 
  • #10
MathematicalPhysicist said:
Did you try it in your machine?

I have Ubuntu 16.04 as the OS in this computer.
I'm retired and no longer have access to MATLAB. I'm thinking about buying it just for these occasions.
 
  • #11
It's not a bug. And the order doesn't matter. The problem is you don't have a dash in there. It's some character that looks like a dash but isn't. When I paste in your code it shows the error message

Error using plot
Error in color/linetype argument.
Error in test1 (line 3)
plot(x,y,'k?','linew',2); See the question mark indicating it doesn't know what that character is? If you get rid of that "dash" and replace it with a real dash (minus sign), it will work perfectly fine.
 
  • Like
Likes   Reactions: MathematicalPhysicist and FactChecker
  • #12
Image Analyst said:
It's not a bug. And the order doesn't matter. The problem is you don't have a dash in there. It's some character that looks like a dash but isn't. When I paste in your code it shows the error message

Error using plot
Error in color/linetype argument.
Error in test1 (line 3)
plot(x,y,'k?','linew',2);See the question mark indicating it doesn't know what that character is? If you get rid of that "dash" and replace it with a real dash (minus sign), it will work perfectly fine.
Good catch! This is one of the frustrating things about copying and pasting text -- they can have characters and do other things you don't want. I like to paste into a programmer editor that I can control, like vim, just to be sure that the text is exactly what I wanted. I often have to replace things that I don't want in code. Then I copy the vim text and paste that into the target code.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K