Search code examples
matlabmatlab-figuredata-annotationsginput

Get subplot number from Ginputc the same way than Ginput (answer with faster Ginput)


I am trying to replace the function ginput with the function ginputc found on fileexchange: Custom GINPUT from Jiro. I find ginput very slow to start when first called. ginputc is faster.

There is one functionality that I was using with ginput that I am not managing to replicate with ginputc. I was getting the subplot number when mouse clicking on it.

This is explained here for ginput: get-subplot-number-from-ginput

But to make it easier, here is some simple code to replicate the functionality:

  figure, hAx_mine(1)=subplot(1, 3, 1); hAx_mine(2)=subplot(1, 3, 2);hAx_mine(3)=subplot(1, 3, 3);
  [x, y, button] = ginput(1);
  [Lia,Locb]=ismember(gca,hAx_mine);
  disp(['Locb gives the subplot number that you have clicked: ' num2str(Locb)])
 

If you try the same in ginputc it always give the last subplot no matter which one you clicked:

  figure, hAx_mine(1)=subplot(1, 3, 1); hAx_mine(2)=subplot(1, 3, 2);hAx_mine(3)=subplot(1, 3, 3);
  [x, y, button] = ginputc(1);
  [Lia,Locb]=ismember(gca,hAx_mine);
  disp(['Locb gives the subplot number that you have clicked: ' num2str(Locb)])

There is an option to get the axe as an extra output from ginputc:

  ...
  [x, y, button,ax] = ginputc(1);
  [Lia,Locb]=ismember(ax,hAx_mine);
  ...

But that is also not working. I suppose that ginputc does change the gca somehow but after many hours trying, I have not managed to find why, and how to fix it.


Solution

  • However... I did not give up but, instead of sorting ginputc, I focused on improving ginput. The initial goal was because ginput was very slow, so why not trying to improve it?

    In that function you have a slow setupFcn, when it's handling the new Matlab toolbar. What setupFcn does is disable the toolbar so it does not show up when you use ginput.

    However this toolbar is not really annoying so why hide it? So I duplicated and renamed ginput to ginput_mine, so I could make changes to it like the following:

    1. I commented the code from line 221 (Disable AxesToolbar) until line 232 (12 lines)
    2. And I also commented a second place in restoreFcn at line 268 (Restore axestoolbar) until line 271 (4 lines)

    What that does is stopping ginput to set toolbarVisible to off (and in 2. restoring the toolbar). This results in a faster function. I have also noticed that it works better because it improves the registering of my clicks, not missing as many of them.

    I am not sure if, with that tempering, all functionalities of ginput will still work, but I would assume so. My tool is an annotation interface to label images, and I am really glad to have made it much smoother for my users :-)

    Update 28 March 2021: Based on the comment from Cris, I thought I might improve my solution by adding an extra step. This extra step is only good if you do not need the toolbar anymore on the figure. What you do is (with above solution) add 2 lines at the start of ginput or just before launching ginput, with Fig your figure:

    set(Fig, 'MenuBar', 'none')
    set(Fig, 'ToolBar', 'none')
    

    I might make it even faster.