save - MATLAB: How to store clicked coordinates using ButtonDownFcn -


goal: perform several clicks in 1 figure, containing image displayed imshow , save coordinates of "clicked" point(s), used in further operations.

notes: know functions getpts/ginput perform without using them. possible using buttondownfcn? (see following code)

function testclicks img = ones(300); % image display h   = imshow(img,'parent',gca); set(h,'buttondownfcn',{@ax_bdfcn});  function ax_bdfcn(varargin) = get(gca,'currentpoint'); x = a(1,1); y = a(1,2); 

at stage variables x , y "live" inside ax_bdfcn. how can make them available in testclicks function? possible using buttondownfcn? approach?

thanks lot.

edit1: answer shai. still cannot accomplish intended.

function [xarray, yarray] = testclicks() img = ones(300); % image display h   = imshow(img,'parent',gca); x = []; y = []; xarray = []; yarray = []; stop = 0; while stop == 0;     set(h,'buttondownfcn',{@ax_bdfcn});     xarray = [xarray x];     yarray = [yarray y];         if length(xarray)>15             stop = 1;         end end      function ax_bdfcn(varargin)         = get(gca, 'currentpoint');         assignin('caller', 'x',  a(1,1) );         assignin('caller', 'y',  a(1,2) );     end end % must have end nested functions 

this code (buggy!) closest can want (after clicking, having array x , y coordinates of clicked points). clealy not understanding mechanics implementation of task. help?

there several ways

  1. using nested functions

    function testclicks    img = ones(300); % image display    h   = imshow(img,'parent',gca);    set(h,'buttondownfcn',{@ax_bdfcn});    x = []; % define "scope" of x , y     y = [];       % call nested function    function ax_bdfcn(varargin)        = get(gca,'currentpoint');        x = a(1,1); % set x , y @ caller scope due "nested"ness of function        y = a(1,2);    end  % close nested function end % must have end nested functions 
  2. using assignin

    function ax_bdfcn(varargin)      = get(gca, 'currentpoint');      assignin('caller', 'x',  a(1) );      assignin('caller', 'y',  a(2) );  
  3. using 'userdata' property of figure handle

    function ax_bdfcn(varargin)      = get(gca, 'currentpoint');      set( gcf, 'userdata', a(1:2) ); 

    'userdata' can accessed (as long figure alive) using cp = get( gcf, 'userdata');.

edit:
example of way "communicate" clicked locations 'base' workspace

function ax_bdfcn(varargin)    = get(gca,'currentpoint');    % hard part - assign points base    if evalin('base', 'exist(''xarray'',''var'')')       xarray = evalin('base','xarray');    else        xarray = [];    end    xarray = [xarray a(1)]; % add point    assignin('base','xarray',xarray); % save base    % same yarray 

after calling testclicks there no xarray or yarray variables in workspace (at least there shouldn't). after first click these 2 variables "miraculously" created. after every other click these 2 arrays increase size until close figure.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -