Reputation: 11
I wrote and am using a matlab program which, among other things, generate a large number of figures usually using the subplot command. [These figures represent molecule trajectories in single molecule experiments, their total number is not known in advance but typically amounts to a few hundreds.] It had worked absolutely smoothly up to this day, where I got the following error message:
Error using subplot (line 159) Requires valid axes handle for input.
After some hand-debugging I think I have isolated the problem as coming from the following sequence:
figure(i)
...
subplot(i)
Where i can be any integer. The easiest reproducible example would be:
i=211;
...
x=linspace(0,1,101);
figure(i)
subplot(211)
plot(x,x)
subplot(212)
plot(x,x)
The problem is identically triggered by setting i=212 in the above case. Obviously I could possibly dirty-quickly fix the problem with some
if i=212 || 211
j=something-different-from-212-or-211;
else
j=i;
end
figure(j)
...
But I would have liked to know if something more convenient/handy/elegant exists – also, I would be curious to know more, if possible, about the cause of this problem!
Thanks!
Upvotes: 1
Views: 1159
Reputation: 1
you can separate the parameters of function subplot:
figure(211);
subplot(2,1,1);
Upvotes: 0
Reputation: 5672
I suspect its a "feature" of the many ways you can call subplot
and the fact that old handles were also known as numbers, for example the following fails:
figure(211); subplot(211)
figure(212); subplot(212)
but:
figure; subplot(211)
figure; subplot(212)
are both okay.
In the 1st one what Matlab is doing is that it is parsing the input arguments to work out which of the ways to process. i.e. the 1st argument can also be an axes handle. e.g.
ax = subplot ( 211 )
% some other code and plot on other subplot
% you can then set the 1st subplot to be active again:
subplot ( ax );
The key item here is that the 1st argument to subplot can be many things...
In your original case where you had:
figure(211);
This means that when you pass in 211
in to the subplot
subplot(211)
It checks to see if its a handle
-> and it is:
figure(211)
ishandle(211)
Then it checks to see if its an axes
-> which is where it fails and triggers the error that you see.
I rarely use subplot at all and when I do I always use the syntax:
hFig = figure;
ax(p) = subplot(m,n,p,'Parent',hFig)
where I have the handle to gui objects and explicitly name them when operating on them.
Upvotes: 2