Elliot
Elliot

Reputation: 5541

Changing LineStyle in Matlab without change being ignored

I am attempting to make lines more easily distinguishable by changing the line styes. However, most of the style changes are being ignored by matlab for no apparent reason. As far as I can tell, only ':' is being acknowledged and shown; everything else just uses a solid line regardless of what I put in. Can anyone explain why this is happening and give a method for making matlab actually change the lines?

hold on;
style = ['-','--',':','-.'];
...
for q = 1:length(names)
   ... %generate data, etc.
   plot_lines(q)=plot(ttt,aclatn,'Color',colors(q,:),'LineStyle',style(1+int8(mod(q,4))),'DisplayName',strcat(num2str(d),'cm'));
end

Upvotes: 2

Views: 207

Answers (1)

Benoit_11
Benoit_11

Reputation: 13945

You need to enclose the line styles in a cell array and access them using {curly braces}. Otherwise Matlab sees your array as one big character array like so:

style =

---:-.

and tries to assign '-' as a LineStyle property 4 times out of 6, which does not work.

However using

style = {'-','--',':','-.'}

to define the styles and

style{1+int8(mod(q,4))}

to access them works fine.

Example:

clear
clc

hold on;
style = {'-','--',':','-.'};

ttt = 1:10;
for q = 1:4
   aclatn = rand(1,10);
   plot_lines(q)=plot(ttt,aclatn,'LineStyle',style{1+int8(mod(q,4))});
end

Output:

enter image description here

Yay!

Upvotes: 2

Related Questions