drawrectangle not working consistently, help!

I am using drawrectangle in an app so the user can interactively draw region of interest. It is not working consistently. Sometimes it works fine, but sometimes it draws the region with a offset in -x direction. I cannot figure out why it works sometimes and somstimes it doesn't. Here is a snippet of my code I am using to get the roi.
function [roi] = selectDataPoints(app,tab)
delete(findall(app.([tab 'UIAxes']), 'Type', 'images.roi.Rectangle'))
originalTitle = get(app.([tab 'UIAxes']).Title, {'String', 'Color'});
set(app.([tab 'UIAxes']).Title, 'String', 'Draw rectangle around region to clip.', 'Color', 'r')
pan(app.([tab 'UIAxes']), 'off') %turn off panning so the interaction doesn't drag the data.
zoom(app.([tab 'UIAxes']),'off')
roi = drawrectangle(app.([tab 'UIAxes']),'StripeColor','r','InteractionsAllowed','none');
% Return original title
set(app.([tab 'UIAxes']).Title, 'String', originalTitle{1}, 'Color', originalTitle{2})
end

10 个评论

This looks like it could be a question for MathWorks Technical Support.
  • What release of MATLAB are you using?
  • How far off in the x-direction is the region? A few pixels, or tens of pixels (or more)?
I am using 2020b with latest update.
The real annoying part is is doesnt happen every time. Here is a graphic showing what it is doing:
What type of axes are you using / how did you produce the map?
I am using UIAxes. Here is what I am using to plot map:
rect_pos = app.([tab '_rect']).Position;
roi_pos = app.([tab '_roi']).Position;
delete(app.([tab 'UIAxes']).Children)
blue = [180/255 198/255 231/255];
green = [83/255 129/255 53/255];
limits{1} = [-180 180];
limits{2} = [0 360];
app.([tab 'UIAxes']).Color = blue;
xlim(app.([tab 'UIAxes']),[limits{app.([tab '_fmt'])}(1) limits{app.([tab '_fmt'])}(2)])
xticks(app.([tab 'UIAxes']),limits{app.([tab '_fmt'])}(1):30:limits{app.([tab '_fmt'])}(2))
xlabel(app.([tab 'UIAxes']),'Longitude')
ylim(app.([tab 'UIAxes']),[-90 90])
yticks(app.([tab 'UIAxes']),-90:15:90)
ylabel(app.([tab 'UIAxes']),'Latitude')
app.([tab 'UIAxes']).XGrid = true;
app.([tab 'UIAxes']).YGrid = true;
mapshow(app.([tab 'UIAxes']),app.Clip_map{app.([tab '_fmt']),1},app.Clip_map{app.([tab '_fmt']),2},'DisplayType','polygon','FaceColor', green)
app.([tab '_rect']) = rectangle(app.([tab 'UIAxes']),'Position', rect_pos,'EdgeColor','r','LineWidth',3,'LineStyle','-.');
app.([tab '_roi']) = drawrectangle(app.([tab 'UIAxes']), 'Position',roi_pos,'StripeColor','r','InteractionsAllowed','none');
This will be difficult to troubleshoot without being able to reproduce the problem.
Perhaps you could attach the app and instructions on how to reproduce the problem or maybe it would be easier to recreate it using uifigure/uiaxes.
I'm curious what ylim returns and what rect_pos and roi_pos are.
To get error, select folder containing *.nc file. Then click drag box and use cursor to drag box on screen. Sometimes it works correctly. If it does, close app and try again.
rect_pos is the rectangle drawing the extent of file, that is drawing correctly (but not using drawrectangle for that, drawing it programatically). It is roi_pos that I am drawing with drawrectangle and having problems with.
I agree with Benjamin Kraus that this should be reported to tech support.
Some observations,
  • When I copy the plot to a regular figure/axes (using copyUIAxes from the file exchange), drawrectangle works without problems.
  • When I copy the uiaxes to a uifigure outside of app designer I get the same problems as we're experiencing in AppDesigner where sometimes it works and sometimes it doesn't.
  • In AppDesigner and within an external UIFigure, 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.
Ok, just put in a technical support ticket.
Would you suggest I remove functionality completely and force user to fat finger in coordinates? Or any other ideas?
Could I open a reagular figure from the app and have user draw rectangle, on figure and then close figure and render roi on uiaxes in app? I dont't know if thats possible...
Questions addressed in my answer below.

请先登录,再进行评论。

 采纳的回答

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:
% Set up figure
uif = uifigure();
uiax = uiaxes(uif);
uiax.UserData.rectangle = [];
grid(uiax,'on')
hold(uiax,'on')
uiax.XLimMode = 'Manual';
uiax.YLimMode = 'Manual';
uiax.ButtonDownFcn = @drawRect;
function drawRect(ax, event)
% User must click twice to mark any two diagonal corners of a rectangle.
if size(ax.UserData.rectangle,1)~=1
% Store the first coordinate
ax.UserData.rectangle = [];
ax.UserData.rectangle(1,:) = event.IntersectionPoint;
else
% Store second coordinate and draw rectangle.
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)
end
end
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.
fig = figure();
appAxes = app.([tab 'UIAxes']);
ax = copyobj(appAxes, fig);
roi = drawrectangle(ax,'StripeColor','r','InteractionsAllowed','none');
appRoi = copyobj(roi, appAxes)
delete(fig)
If you're using an older release of matlab that does not support uiaxes with copyobj, you can use copyUIAxes() from the file exchange.

4 个评论

Can you provide an example to Alternative 2, I am trying to figure it out but need a little help in right direction... With the ButtonDownFunction and CurrentPoint...
I am trying to use your Alternative 3, and for some reason my handle to rectangle is deleted after I leave the that function even though I am storing handle in app.([tab '_roi'). Just somethine else that will take me hours to resolve...
Thanks for giving me options in directions I can go to solving the overall problem!!
Thanks for all your help, I was getting frustrated there (figured out the deleted roi, lol)!
An example of Alt#2 is added to my answer.
Make sure you're storing the handle to the copied object (appRoi in my demo) rather than the handle that's deleted with the figure (roi in my demo).

请先登录,再进行评论。

更多回答(2 个)

I had the same Problem, when I was using the AppDesigner (Matlab 2019a). I used a GridLayout to organize the axes and some surrounding UI-Objects within a tab. I've found out, that the padding of the GridLayout of the Tab was causing the Offset (Parent of axes). I changed the padding to [0,0,0,0] and the offset was gone. When I increased the padding of GridLayout the offset got even worse. Hope, this may help you.
Best Christian
is this still an issue? I remember having also problems with that, but at the moment, I cannot replicate the problem in 2022a
f=uifigure; ax=uiaxes(f)
drawrectangle(ax)
seems to work just fine

1 个评论

I'm curious as to what version the ROI tools were even usable at all in a uifigure. As of R2019b, besides the extreme lag, the context menu doesn't even work in a uifigure. If I had to rely on release notes, I'd have to assume they're still not completely functional in a uifigure.
I'd try MATLAB online, but it's not loading for me today again.

请先登录,再进行评论。

产品

版本

R2020b

标签

Community Treasure Hunt

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

Start Hunting!

Translated by