Try setting the DataAspectRatio to [1 1 1] using axis(uiax,'equal').
If that doesn't fix the issue, it would help to share a bit more about how you are plotting the image.
> how can I get just part of the image that is in view?
Method 1: capture the cropped axes
- Pros: quick and easy
- Cons: This copies the image portentially at a different resolution
Here's a function that receives an axes handle and creates of copy of the content in a new axes.
function exportClippedImage(ax)
originalState = ax.Visible;
revertVis = onCleanup(@()set(ax,'Visible',originalState));
imshow(F.cdata,'Parent',newax)
Method 2: index the image
- Pros: uses the original image data
- Cons: Much more work and it's only applied to the image object, ignores anything else in the axes
- Use the xlim and ylim to crop the original image data. What makes this difficult is that the image pixels are centered on a coordinates and the coordinates are determined by the first and last XData and YData values. The xlim and ylim can split a pixel but your indexing cannot.
- Once you have the cropped CData, use it to generate a new image in a new axes.
- Ensure the new axes has an appropriate aspect ratio to match that of the original axes and adjust any other properties.
Demo
set(uiax,'xtick',[],'ytick',[]);
xhalfPixWidth = diff(I.XData)/(size(I.CData,2)-1)/2;
yhalfPixWidth = diff(I.YData)/(size(I.CData,1)-1)/2;
imageXBounds = sort(I.XData([1,end]))+[-1 1]*xhalfPixWidth;
imageYBounds = sort(I.YData([1,end]))+[-1 1]*yhalfPixWidth;
imageXIdx = round((xl-imageXBounds(1))./diff(imageXBounds).*(imgSize(2)-1)+1);
imageYIdx = round((yl-imageYBounds(1))./diff(imageYBounds).*(imgSize(1)-1)+1);
imageXIdx = [max(imageXIdx(1),1), min(imageXIdx(2),imgSize(2))];
imageYIdx = [max(imageYIdx(1),1), min(imageYIdx(2),imgSize(1))];
newimage = I.CData(imageYIdx(1):imageYIdx(2), imageXIdx(1):imageXIdx(2));
image(newax, uiax.XLim, uiax.YLim, newimage, 'CDataMapping',I.CDataMapping)
colormap(newax, uiax.Colormap)
newax.PlotBoxAspectRatio = uiax.PlotBoxAspectRatio;
newax.DataAspectRatio = uiax.DataAspectRatio;
axis(newax,'equal','off')
title('Copped and copied')
Notice the the zoom in my demo captures portions of the rows and columns at the borders. My algorithm includes those full rows and column. You may need to set other properties to control the final copied image but this should get you started.