在Matlab中绘制多条线(在单元格阵列中通过线型循环) [英] Plot several lines (looping through line styles in cell array) in Matlab
问题描述
我已编写此循环以绘制结果的每一行,并且收到错误消息
I have written this loop to plot each line of results and I get the error message
使用图时出错.无效的第一个数据参数.
Error using plot. Invalid first data argument.
到目前为止看起来像这样
So far it looks like this
test=rand(5,6);
xint=[1:1:6];
LineSpec = {'-y', '--m', ':c', '-r.', '-b', ':s'};
for ii=1:5,
plot(xint,test(ii,:),LineSpec(ii),'linewidth',2);
hold on;
legend_str{ii} = num2str(ii);
end
如果我使用plot(xint,test(ii,:),'-y','linewidth',2)
,那么它将起作用.但是如何在遍历线型时避免错误?
If I use plot(xint,test(ii,:),'-y','linewidth',2)
then it works. But how can I avoid the error when looping through line styles?
推荐答案
您应该写:
plot(xint,test(ii,:),...
LineSpec{ii},...
'linewidth',2);
LineSpec
是一个单元格数组,所以LineSpec(ii)
返回一个单元格,而plot
要求一个字符数组作为行属性.
LineSpec
is a cell array, so LineSpec(ii)
returns a cell, while plot
asks for a character array as line properties.
您可以在呼叫LineSpec
时看到区别:
you can see the difference when you call LineSpec
:
>> LineSpec{1}
ans =
-y
>> LineSpec(1)
ans =
'-y'
当输出为单元格时,答案会缩进并带有单引号.
When the output is a cell then the answer is indented and has the single-quote marks.
这篇关于在Matlab中绘制多条线(在单元格阵列中通过线型循环)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!