Why dashed lines and dotted lines not shown as set required, but replaced with solid lines?

3 visualizaciones (últimos 30 días)
ls=['-',':','--',':'];
lc=['b','r','b','k'];
for ii=1:4
figure(1);
plot(t1,CPL1,[ls(ii) lc(ii)],'LineWidth',3);
hold on
end
I just simply this loop, this loop will produce figures as per one time, but 3nd round not produce dashed line, but solid lines, the 4th round not show dotted line but solid line too. 1st and 2nd curves are solid and dotted, correct. I am wondering why. Can anybody answer this question? I spent a few days to think about it, but dont know how ... Thanks

Respuestas (2)

Star Strider
Star Strider el 28 de Mzo. de 2017
You need to create ‘ls’ as a cell array, and then address it as a cell array. The square brackets concatenate strings, and the cell array keeps them separate. It does not matter if ‘lc’ are concatenated, since the individual letters are treated as separate elements of the vector. You can create a cell array from them as well if you want to, but it will not affect your code.
The ‘ls’ array becomes:
ls={'-',':','--',':'};
and the plot call becomes:
plot(t1,CPL1,[ls{ii} lc(ii)],'LineWidth',3);
Note the curly brackets ‘{}’ for cell array addressing.

Image Analyst
Image Analyst el 28 de Mzo. de 2017
You're plotting all the lines on top of one another and all the lines have the very same coordinates. Just set a breakpoint on the hold line and you'll see what's going on. So the first line is blue solid. Then the next plot() places a red dashed line on top, and you can still see the blue solid underneath it. Then you come along and place a blue dashed line on top (or at least you will once you fix it). Well, where there should be white space in the gaps of the dashes, you can see the blue solid line from the first plot. If you don't want that, then don't call hold on. Or else change t and CPL1 on each iteration.
Then, like Star said, you need to use cell arrays, not character arrays because your ls(3) is '-', NOT '--' since you're just extracting a single character, not a pair of characters. If you use a cell array, ls{3} will be '--', which is what you want. See the FAQ: http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F
This works:
ls={'-',':','--',':'}
lc={'g','r','b','k'}
for ii=1:4
figure(1);
t1 = randi(9, 1, 12) % Sample data
CPL1 = rand(1,12);
plot(t1,CPL1,[lc{ii},ls{ii}],'LineWidth', 3);
hold on
end

Categorías

Más información sobre Annotations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by