这是我尝试并排绘制的 2 个函数:
numgraphs = 2;
x = 1:5;
y1 = x.^2;
y2 = x.^3;
funcs = cell(y1, y2);
for i=1:numgraphs
subplot(1,2,i);
plot(x,funcs(i));
end
但我得到了这个错误:
Error using plot
Conversion to double from cell is not possible.
我正在尝试做的事情可能吗?
这是我尝试并排绘制的 2 个函数:
numgraphs = 2;
x = 1:5;
y1 = x.^2;
y2 = x.^3;
funcs = cell(y1, y2);
for i=1:numgraphs
subplot(1,2,i);
plot(x,funcs(i));
end
但我得到了这个错误:
Error using plot
Conversion to double from cell is not possible.
我正在尝试做的事情可能吗?
您的代码中有两个问题:
funcs = {y1, y2};,而不是funcs = cell(y1, y2);plot(x,funcs{i});,而不是plot(x,funcs(i));。花括号用于访问单元格的内容。带括号的单元格索引()返回一个单元格数组,而不是此单元格中包含的函数:
>> x = {1};
>> class(x(1))
ans =
cell
>> class(x{1})
ans =
double
你想要{}索引:
plot(x,funcs{i});
有关详细信息,请参阅 http://www.mathworks.de/de/help/matlab/matlab_prog/access-data-in-a-cell-array.html 。