In AppDesigner / uifigure & uiaxes, drawrectangle is horribly slow and does not show the rectangle borders immediately. It's so slow that I would never use it in AppDesigner or a uifigure. I think this giagantic lag is the cause of the problem. As evidence of that, if you hold the initial button-down in place, slowly drag, and then hold the mouse button in the final destination until the rectangle appears, and then let go of the mouse button, the problem goes away but it's replaced by an unreasonable amount of waiting and wondering and frustration.
The problem of slow responses with uifigures / uiaxes / AppDesigner is common
Alternative 1) Provide the user with instructions to not let go of the mouse until the rectangle appears.
Alternative 2) Set the axes' ButtonDownFunction along with the CurrentPoint to detect coordinates of button-down and button-up and then can plot the rectangle manually using rectangle() or plot() etc.
Demo of Alernative 2:
uiax.UserData.rectangle = [];
uiax.XLimMode = 'Manual';
uiax.YLimMode = 'Manual';
uiax.ButtonDownFcn = @drawRect;
function drawRect(ax, event)
if size(ax.UserData.rectangle,1)~=1
ax.UserData.rectangle = [];
ax.UserData.rectangle(1,:) = event.IntersectionPoint;
ax.UserData.rectangle(2,:) = event.IntersectionPoint;
WidthHeight = abs(diff(ax.UserData.rectangle(:,1:2)));
rectangle('Parent',ax,'Position',...
[min(ax.UserData.rectangle(:,1)), min(ax.UserData.rectangle(:,2)), WidthHeight], ...
'EdgeColor','r','LineWidth',2)
Alternative 3) Use an external figure that wasn't created by uifigure/uiaxes
Demo of Alternative 3:
Use copyobj to copy the axes to a new figure. The new figure will be a regular figure but the copied axes will still be uiaxes but I don't think that will be a problem. Then draw the rectangle and copy it back to the app's axes.
appAxes = app.([tab 'UIAxes']);
ax = copyobj(appAxes, fig);
roi = drawrectangle(ax,'StripeColor','r','InteractionsAllowed','none');
appRoi = copyobj(roi, appAxes)
If you're using an older release of matlab that does not support uiaxes with copyobj, you can use copyUIAxes() from the file exchange.
Best Answer