imagesc does not take an axis handle as its first argument even if it should

7 views (last 30 days)
According to the documentation (https://www.mathworks.com/help/matlab/ref/imagesc.html), imagesc should take as an optional first argument an axis handle. However, I get errors when I try this:
fig = figure;
ax = fig.CurrentAxes;
imagesc(ax, [1,2;3,4])
The output is this:
>> test
Dot indexing is not supported for variables of this type.
Error in imagesc (line 48)
elseif any(strcmpi(cax.NextPlot,{'replaceall','replace'}))
Error in test (line 3)
imagesc(ax, [1,2;3,4])
Using a much bigger script, I also got the following error, which I am unable to reproduce with a short script:
Error using image
Incorrect number of arguments.
Error in imagesc (line 40)
hh = image(varargin{:},'CDataMapping','scaled');
Error in testbed2_generateF (line 93)
imagesc(ax, [N_BS(1), N_BS(end)], [N, N], log10(errF))

Accepted Answer

Steven Lord
Steven Lord on 19 Sep 2018
Look at the variable ax.
>> fig = figure;
ax = fig.CurrentAxes
ax =
0×0 empty GraphicsPlaceholder array.
That's not an axes. When you create a figure, it doesn't have an axes on it unless you've specifically set up a CreateFcn or something similar that adds an axes. [When you call a plotting function like plot it creates the figure then creates an axes on that figure and finally creates the plot on the axes.] Compare with:
>> fig = figure;
axes(fig);
ax = fig.CurrentAxes
ax =
Axes with properties:
...
The error message could probably stand to be a bit more descriptive. If you feel strongly about that, I recommend submitting that to Technical Support as an enhancement request using the Contact Us link in the upper-right corner of this page.
  1 Comment
Alec Poulin
Alec Poulin on 20 Sep 2018
Hi, this is a very good answer, thank you!
I just noticed that in my longer script, I use hold on right after fig = figure;, which seems to add axes in the figure. Without hold on, all the xlim, ylim, plot, and contour functions of the script return errors. The funny part is that these errors are not the same:
  • xlim and ylim say Invalid or deleted graphics object.
  • plot says Parent must be a scalar graphics handle.
  • contour says Input arguments must be real. Use the function REAL to get the real part of the inputs.
Now my question is how to make the call to imagesc work. I thought the problem was caused by ax, because it worked when I removed it. I will have to investigate a little bit more.

Sign in to comment.

More Answers (0)

Tags

Products


Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!