0

I have two axes: one for viewing images and the other for plotting graphs. I get this error when I try to specify which axes I want to plot the data on: Error using plot. A numeric or double convertible argument is expected when trying 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

You can paste this example into Matlab to try it out. How can the code be corrected in order to specify the axes?

Senyokbalgul
  • 1,058
  • 4
  • 13
  • 37

2 Answers2

1

plot in the curve fitting toolbox is not the same as MATLAB's base plot. Though there is a documented syntax for specifying the parent axes for sfit objects, there doesn't seem to be one for cfit objects, which would be returned by your fit call in this case.

However, from the documentation we see that:

plot(cfit) plots the cfit object over the domain of the current axes, if any

So if the current axis is set prior to the plot call it should work as desired. This can be done either by modifying the figure's CurrentAxes property or by calling axes with the handle of an axes object as an input.

% 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);
sco1
  • 12,154
  • 5
  • 26
  • 48
  • Your solution looks exactly the same as the erroneous code from above. Can the axes even be specified if using the `plot` in the curve fitting toolbox? – Senyokbalgul Aug 27 '16 at 16:53
  • @Senyokbalgul I have updated my response, I didn't notice that MATLAB made a distinction between plotting `sfit` and `cfit` objects. – sco1 Aug 27 '16 at 19:07
1

I refine my answer as follows:

It looks that the plot function in does not like a fit object after an axis followed by two vectors. In such case, I would do something like this:

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

Actually the trick is to provide x and then curve(x) as two vectors without giving the whole fit-object to the plot function.

NKN
  • 6,482
  • 6
  • 36
  • 55