matlab - Speed up creation of impoint objects -
i have create draggable points on axes. however, seems slow process, on machine taking bit more second when done so:
x = rand(100,1); y = rand(100,1); tic; = 1:100 h(i) = impoint(gca, x(i), y(i)); end toc;
any ideas on speed highly appreciated.
the idea provide user possibility correct positions in figure have been calculated matlab, here exemplified random numbers.
you can use the ginput
cursor within while loop mark points want edit. afterwards click outside axes leave loop, move points , accept key.
f = figure(1); scatter(x,y); ax = gca; = 1; while 1 [u,v] = ginput(1); if ~inpolygon(u,v,ax.xlim,ax.ylim); break; end; [~, ind] = min(hypot(x-u,y-v)); h(i).handle = impoint(gca, x(ind), y(ind)); h(i).index = ind; = + 1; end
depending on how you're updating plot can gain general speedup using functions clf
(clear figure) , cla
(clear axes) instead of opening new figure window explained in this answer may useful.
alternatively following rough idea of meant in comments. throws various errors , don't have time debug right now. maybe helps starting point.
1) conventional plotting of data , activating of datacursormode
x = rand(100,1); y = rand(100,1); xlim([0 1]); ylim([0 1]) f = figure(1) scatter(x,y) datacursormode on dcm = datacursormode(f); set(dcm,'displaystyle','datatip','enable','on','updatefcn',@customupdatefunction)
2) custom update function evaluating chosen datatip , creating impoint
function txt = customupdatefunction(empt,event_obj) pos = get(event_obj,'position'); ax = get(event_obj.target,'parent'); sc = get(ax,'children'); x = sc.xdata; y = sc.ydata; mask = x == pos(1) & y == pos(2); x(mask) = nan; y(mask) = nan; set(sc, 'xdata', x, 'ydata', y); set(datacursormode(gcf),'enable','off') impoint(ax, pos(1),pos(2)); delete(findall(ax,'type','hggroup','handlevisibility','off')); txt = {};
it works the, if you'd want move 1 point. reactivating datacursormode , setting second point fails:
maybe can find error.
Comments
Post a Comment