MATLAB: Invalid Handle Object with Subplot

handle graphicsMATLABsetsubplot

Hi, I would appreciate any help on this matter, thank you for reading 🙂
I am attempting to do something like the following:
x1 = [1 2 3 4];
y1 = [2 4 6 8];
y2 = [4 8 12 16];
figure; hold on;
h1 = subplot(1,2,1);
hold on;
plot(x1,y1)
h2 = subplot(1,2,2);
hold on;
plot(x1,y2);
set(h1,'title','test1');
set(h2,'title','test2');
However, I get the error:
??? Error using ==> set Value must be a handle
I have also attempted to set both the figure and subplot properties to include 'NextPlot','add' but this did not help.
Thanks for all suggestions!

Best Answer

The problem is that the property title requires a handle to a text object and not a string. The help http://www.mathworks.com/help/releases/R2011a/techdoc/ref/axes_props.html deals with this. You can create a text object in the appropriate axis with:
axes(h1);
set(h1,'title',text('string', 'test1'));
axes(h2);
set(h2,'title',text('string', 'test2'));
You might just want to use:
title(h1, 'test1');