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

조회 수: 61 (최근 30일)
Kurt
Kurt 2022년 12월 12일
댓글: Voss 2022년 12월 13일
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일
app.UIAxes.XLim = [0 1000];
app.UIAxes.YLim = [0 1000];
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.
  댓글 수: 2
Kurt
Kurt 2022년 12월 13일
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);

댓글을 달려면 로그인하십시오.

추가 답변 (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