为什么我的图不能针对不同的图形表示循环工作?

Why my plot is not working in loop for different graph representations?

我有一个矩阵 'capacity',我想绘制它的行,为此我使用了一个循环,我的代码是

for j_1=1:8    
plotStyle = {'k -','r +','g *','b.','y o','r--','b d','g s'};   
hold on;
plot(x_1,capacity(j_1,:),plotStyle(j_1));
end
hold off;

x_1 只是 x 轴,x_1 中的元素数等于 capacity.But 的列数我得到的错误是:

使用绘图时出错

第一个数据参数无效

变化扩散系数错误(第 124 行)

plot(x_1,capacity(j_1,:),plotStyle)

编辑:您需要做的就是在绘图调用中用大括号替换圆括号,即

plot(x_1,capacity(j_1,:),plotStyle{j_1});

或者,您可以将颜色和线型分开,然后以这种方式进行调用。当您绘制更大的图并希望通过颜色和线型的组合以不同方式循环时,这可能会很方便。

capacity = rand(8,8);     % test data for a workable example
x_1 = 1:8;

for j_1=1:8    
linestyle = {'-','+','*','.','o','--','d','s'};   
color = {'k','r','g','b','y','r','b','g'};   
hold on;
plot(x_1,capacity(j_1,:),'color',color{j_1},'linestyle',linestyle{j_1});
end
hold off;

您的问题是访问带有普通括号 () 的元胞数组,您应该使用花括号 {}

plotStyle = {'k -','r +','g *','b.','y o','r--','b d','g s'};  
% plotStyle(1) = {'k -'}   : cell
% plotStyle{1} = 'k -'     : string

所以

hold on;  % Move the hold and style assignment outside the loop for efficiency
plotStyle = {'k -','r +','g *','b.','y o','r--','b d','g s'};
for j_1=1:8        
    plot(x_1,capacity(j_1,:),plotStyle{j_1});
end
hold off;

Documentation link for accessing data in a cell array