使用函数`fit`时如何指定轴

时间:2016-08-27 16:29:45

标签: matlab user-interface plot matlab-guide multiple-axes

我有两个轴:一个用于查看图像,另一个用于绘制图形。当我尝试在Error using plot. A numeric or double convertible argument is expected尝试指定我想要绘制数据的轴时plot(handles.axis,curve,x,y),我收到此错误。

figure
handles.axis = gca;
x = 1:10;
y = 1:10;
curve = fit(x',y','linearinterp');
plot(curve,x,y) % works fine
plot(handles.axis,curve,x,y) % doesn't work
plot(curve,x,y,'Parent',handles.axis)  % doesn't work

您可以将此示例粘贴到Matlab中进行试用。如何更正代码以指定轴?

2 个答案:

答案 0 :(得分:1)

plot in the curve fitting toolboxMATLAB's base plot不同。虽然有一个用于指定sfit个对象的父轴的文档化语法,但cfit个对象似乎不是一个,fit调用将返回plot(cfit)个对象。这种情况。

但是,从文档中我们看到:

  

cfit在当前轴的域上绘制plot对象,如果有的话

因此,如果在CurrentAxes调用之前设置了current axis,那么它应该可以正常工作。这可以通过修改图形的% Set up GUI h.f = figure; h.ax(1) = axes('Parent', h.f, 'Units', 'Normalized', 'Position', [0.07 0.1 0.4 0.85]); h.ax(2) = axes('Parent', h.f, 'Units', 'Normalized', 'Position', [0.55 0.1 0.4 0.85]); % Set up curve fit x = 1:10; y = 1:10; curve = fit(x', y', 'linearinterp'); % Returns cfit object axes(h.ax(2)); % Set right axes as CurrentAxes % h.f.CurrentAxes = h.ax(2); % Set right axes as CurrentAxes plot(curve, x, y); 属性或通过以轴对象的句柄作为输入调用axes来完成。

XPath

答案 1 :(得分:1)

我的答案如下:

看起来中的plot函数在轴跟随两个向量之后不喜欢拟合对象。在这种情况下,我会做这样的事情:

x = 1:10;
y = 1:10;
figure % new figure
ax1 = subplot(2,1,1);
ax2 = subplot(2,1,2);

curve = fit(x',y','linearinterp');
plot(ax1,x,curve(x));
hold on;plot(ax1,x,y,'o') % works fine

plot(ax2,x,curve(x));
hold on;plot(ax2,x,y,'o') % works fine

实际上诀窍是提供x然后curve(x)作为两个向量而不将整个fit-object赋予plot函数。

相关问题