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

73 次查看(过去 30 天)
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 个评论
Voss
Voss 2022-12-13
编辑:Voss 2022-12-13
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.

请先登录,再进行评论。

采纳的回答

Voss
Voss 2022-12-13
编辑:Voss 2022-12-13
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by