imshow breaks after changing uiaxes limits
6 views (last 30 days)
Show older comments
I wanted to plot a figure and then replace it with an image at a later time. The following code runs perfectly fine:
ax = uiaxes;
currentimage = imread(imagepath);
imshow(currentimage, 'Parent', ax);
but as soon as i try to change the limits of one of the axes before showing the image
ax.XLim = [0.002 .02];
the figure doesn't show. I just have a blank figure window.
One workaround is to not use uiaxes (just using ax = axes works fine), but I was working on an app in AppDesigner so it would be nice. Another I thought might be to just hide the axes and create new ones? Either way it was frustrating working out how one little change broke my program...
Is this a bug? What's happening?
0 Comments
Accepted Answer
DGM
on 29 Jun 2022
Edited: DGM
on 29 Jun 2022
You're specifying that the axes limits should be outside of the image region.
currentimage = imread('cameraman.tif');
hi = imshow(currentimage);
hi.XData % this is the Xrange of the image data
ax = gca;
ax.XLim = [0.002 .02]; % but this is where the axis limits are
You can explicitly specify the extent of the image in the call to imshow() by including 'xdata' and 'ydata' parameters. See the documentation for imshow() for more details. Bear in mind that the aspect ratio is 1 when using imshow(), so even if you set the xdata for the image to [0.002 0.2], you'll wind up trying to plot an image that's 1280 times as tall as it is wide. It will likely be so narrow that it simply isn't visible. You'll have to pay attention to both x and y limits if you want to keep the image in-axes and viewable.
2 Comments
More Answers (0)
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!