Speed ​​up object creation

I need to create multiple dragged points on an axis. However, this seems to be a very slow process, takes a little over a second on my machine when done like this:

x = rand(100,1);
y = rand(100,1);

tic;
for i = 1:100
    h(i) = impoint(gca, x(i), y(i));
end
toc;

      

Any ideas for speeding up would be much appreciated.

The idea is to provide the user with the ability to adjust the positions in the figure that was previously calculated by Matlab, here are random numbers.

+3


source to share


1 answer


You can use the cursor in a while loop to mark all the points you want to edit. Then just click outside of the axes to exit the loop, move the points and accept any key. ginput

f = figure(1);
scatter(x,y);
ax = gca;
i = 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;
        i = i + 1;
 end

      

enter image description here


Depending on how you update your plot, you might get an overall speedup with functions like (clear digit) and (clear axes) rather than opening a new shape window as explained in this answer might be helpful. clf

cla


Alternatively, this is a very rough idea of ​​what I had in mind in the comments. It throws various errors and I don't have time to debug it right now. But maybe it helps as a starting point.



1) Normal data building and datacursormode activation

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 that evaluates the selected datatype and creates 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 for if you just want to move one point. Reactivating datacursormode and setting the second point fails:

enter image description here

Perhaps you can find the error.

+1


source







All Articles