How to draw lines in UIFigure designed in App Designer that contains an image

90 views (last 30 days)
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.
  10 Comments
Voss
Voss on 13 Dec 2022
Edited: Voss on 13 Dec 2022
The image goes in an axes, same as the lines. Like I said before:
"One thing that should work for what you want to do is to use an image (which must be in an axes/uiaxes) instead of a uiimage (which must be in a uifigure/uipanel/etc.)."
I'm not sure what the panel is for; you may not need it.

Sign in to comment.

Accepted Answer

Voss
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
Kurt
Kurt on 13 Dec 2022
It worked! Thanks.
C = imread("pattern.png");
app.UIAxes = uiaxes(app.UIFigure);
app.UIAxes.XLim = [0 1000];
app.UIAxes.YLim = [0 1000];
img = image(app.UIAxes, C);

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!