imshow breaks after changing uiaxes limits

6 次查看(过去 30 天)
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?

采纳的回答

DGM
DGM 2022-6-29
编辑:DGM 2022-6-29
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
ans = 1×2
1 256
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 个评论
Andrew Estrada
Andrew Estrada 2022-6-29
The most annoying thing is I've had an issue like this (someone else had posted about) with a similar answer in the past. Thanks!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by