Hi @Marvellous,
You can place an image at a specific position on the axes in MATLAB R2023b using the image() function with 'XData'and 'YData'. This allows you to set either the upper-left corner or center point at coordinates like (1,3). For example:
% Upper-left corner at (1,3)
image('XData', [1, 1 + width - 1], 'YData', [3, 3 + height - 1], 'CData', img); % Center at (1,3)
image('XData', [1 - width/2, 1 + width/2], 'YData', [3 - height/2, 3 + height/2],
'CData', img);This method gives precise control over the image placement while preserving axes coordinates and aspect ratio.
Reference:
<https://www.mathworks.com/help/matlab/ref/image.html Matlab Image Documentation >
The script I provided demonstrates:
1. Image placement by upper-left corner at (1,3). 2. Image placement by center at (1,3). 3. An overlay figure showing both placements with annotations and a legend for visual verification.
Implementation
%% MATLAB Script: Display Image at a Designated Position with Annotations % Author: Umar % Date: 2025-08-19 % Purpose: Show image at specific coordinates and annotate positions % Image path: /MATLAB Drive/Cat.png
%% Step 1: Read the image imagePath = '/MATLAB Drive/Cat.png'; img = imread(imagePath); [height, width, ~] = size(img);
%% Step 2: Display image using upper-left corner upperLeftX = 1; upperLeftY = 3;
figure('Name','Image Placement - Upper Left Corner','Color','w');
image([upperLeftX, upperLeftX + width - 1], [upperLeftY, upperLeftY + height
- 1], img);
set(gca, 'YDir', 'normal');
axis equal;
axis on;
grid on;
xlabel('X-axis');
ylabel('Y-axis');
title('Image placed using upper-left corner at (1,3)'); % Annotate upper-left corner
text(upperLeftX, upperLeftY, sprintf(' Upper-left corner (%.1f, %.1f)',
upperLeftX, upperLeftY), ...
'Color','red','FontWeight','bold','FontSize',12,'VerticalAlignment','top');%% Step 3: Display image using center point centerX = 1; centerY = 3;
figure('Name','Image Placement - Center Point','Color','w');
image([centerX - width/2, centerX + width/2], [centerY - height/2, centerY +
height/2], img);
set(gca, 'YDir', 'normal');
axis equal;
axis on;
grid on;
xlabel('X-axis');
ylabel('Y-axis');
title('Image placed using center point at (1,3)');
% Annotate center point
text(centerX, centerY, sprintf(' Center (%.1f, %.1f)', centerX, centerY), ...
'Color','blue','FontWeight','bold','FontSize',12,'VerticalAlignment','middle');
%% Step 4: Optional overlay with annotations
figure('Name','Overlay Example with Annotations','Color','w');
hold on;
% Upper-left corner
image([upperLeftX, upperLeftX + width - 1], [upperLeftY, upperLeftY + height -
1], img);
text(upperLeftX, upperLeftY, sprintf(' Upper-left (%.1f, %.1f)', upperLeftX,
upperLeftY), ...
'Color','red','FontWeight','bold','FontSize',12,'VerticalAlignment','top');
% Center point with transparency
alpha = 0.5;
imgDouble = im2double(img);
image([centerX - width/2, centerX + width/2], [centerY - height/2, centerY +
height/2], imgDouble, 'AlphaData', alpha);
text(centerX, centerY, sprintf(' Center (%.1f, %.1f)', centerX, centerY), ...
'Color','blue','FontWeight','bold','FontSize',12,'VerticalAlignment','middle');
% Set axes properties
set(gca, 'YDir', 'normal');
axis equal;
axis on;
grid on;
xlabel('X-axis');
ylabel('Y-axis');
title('Overlay: Upper-left and Center Placement with Annotations');
% Create dummy plot handles for legend to avoid warnings
h1 = plot(nan, nan, 's', 'MarkerFaceColor','red','MarkerEdgeColor','red');
h2 = plot(nan, nan, 's', 'MarkerFaceColor','blue','MarkerEdgeColor','blue');
legend([h1, h2], {'Upper-left corner','Center point'});
hold off;
Please see attached.




