How to draw lines in UIFigure designed in App Designer that contains an image
    38 views (last 30 days)
  
       Show older comments
    
I have seen many examples of how to draw a line in a plot; however, I am working with an App Designer object-oriented UIFigure. The figure contains an image - No axes or any other components. How do I draw lines overlaying my image, from an outside script?
I was using the old bitmapped approach, changing the color of bits in the image, but that way is horribly slow. 
classdef sky_frame < matlab.apps.AppBase
    properties (Access = public)
        UIFigure    matlab.ui.Figure
        Panel       matlab.ui.container.Panel
        Image       matlab.ui.control.Image
    end
    methods (Access = Public)
        function drawLine(app)              % function called externally to draw lines
            global xvect; global yvect);
            line(xvect, yvect, 'Color', 'yellow');
        end    
    end
    methods (Access = private)
        function createComponents(app)
            app.UIFigure = uifigure('Visible', 'off');
            app.Panel = uipanel(app.UIFigure);
            app.Image = uiimage(app.Panel);
            img = "pattern.png";
            app.Image.ImageSource = imread(img);
            app.UIFigure.Visible = 'on';    
        end
    end
    methods (Access = public)
        function app = sky_frame
            createComponents(app);
        end
    end
end
Elsewhere in my code I instantiate my sky_frame:
function scan = initialize_sky()
    global scan;
    scan = sky_frame;
end
I call a function that draws four vectors to create a rectangle. I have to use this approach because my rectangles may be tilted at various angles.
function draw_box
    global scan;                    % sky_frame object
    global xvect; global yvect;     % vectors passed to drawLine function
    x1 = 141.;
    x2 = 141.;
    y1 = 491.;
    y2 = 508.;
    xvect = [x1 x2]; yvect = [y1 y2];
    scan.drawLine();                % (I could probably dispense with the globals here)
end
At this point, a new plot window pops up with my line in it. I want  the line to be in my "scan" object, not in the plot window.
How do I set the current figure handle to point to my scan object instead of the plot window? I have played around with gcf and gca, but they don't seem to apply to my sky_frame, which only contains an image - no axes. Including the draw_line function in the sky_frame definition as a method is not helping, apparently.
Accepted Answer
  Voss
      
      
 on 13 Dec 2022
        
      Edited: Voss
      
      
 on 13 Dec 2022
  
      See the example below.
% axes and image
ax = axes();
im = image(ax,'CData',randi([1 255],100,100));
ax.XLim = [0.5 100.5];
ax.YLim = [0.5 100.5];
ax.Visible = 'off';
% line
line(ax,[20 50 85],[50 20 85],'Color','g','LineWidth',10)
The syntax is the same for uiaxes.
2 Comments
More Answers (0)
See Also
Categories
				Find more on Creating, Deleting, and Querying Graphics Objects 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!


