Thread Subject: Axes tools in GUI

Subject: Axes tools in GUI

From: David Doria

Date: 26 Jun, 2008 22:20:19

Message: 1 of 15

I have an axes object on my GUI that I made in guide. I can
draw things in it with "plot" or other commands like that,
but I would like to be able to zoom and select points and
things like that also.

1) can I get the normal toolbar that would be above the axes
in a figure window in my gui?

2) is there a "axes clicked" type of callback that I can do
something when the user selects a point in the axes?

Thanks,

Dave

Subject: Axes tools in GUI

From: rodney.thomson@gmail.com

Date: 27 Jun, 2008 01:29:42

Message: 2 of 15

On Jun 27, 6:20=A0am, "David Doria" <daviddo...@gmail.com> wrote:
> I have an axes object on my GUI that I made in guide. =A0I can
> draw things in it with "plot" or other commands like that,
> but I would like to be able to zoom and select points and
> things like that also.
>
> 1) can I get the normal toolbar that would be above the axes
> in a figure window in my gui?
>
> 2) is there a "axes clicked" type of callback that I can do
> something when the user selects a point in the axes?
>
> Thanks,
>
> Dave

1) You can have your overall GUI have the toolbar that you desire.
Using guide double click on the background of yoru GUI (typically the
figure1 property) and change the value for 'Toolbar' to 'figure'. Else
you can do "set(handles.figure1, 'Toolbar', 'figure')" in your GUI
startup function.

2) As for the 'point in the axes' do you mean A) ANYWHERE on the axis?
or B) a plotted data point within an axes?

If A : you can set the axes' ButtonDownFcn property to be a callback
function of your own desire. IE : set(handles.axes1, 'ButtonDownFcn',
@disp('You clicked!'))

If B then you want to do similar, but set it on your plot properties :)

Subject: Axes tools in GUI

From: David

Date: 1 Jul, 2008 16:36:03

Message: 3 of 15

I am actually using imshow to produce the plot, so I'd like
to get the coordinate of the pixel I click on.

I think that is simply the axes1.CurrentPoint property?

So I did this:

function axes1_ButtonDownFcn(hObject, eventdata, handles)
disp('you clicked!')
disp(handles.axes1.CurrentPoint)


but it never gets called when I click on the plot!?

Subject: Axes tools in GUI

From: Steven Lord

Date: 1 Jul, 2008 16:44:47

Message: 4 of 15


"David " <daviddoria@gmail.com> wrote in message
news:g4dmdi$a0u$1@fred.mathworks.com...
>I am actually using imshow to produce the plot, so I'd like
> to get the coordinate of the pixel I click on.
>
> I think that is simply the axes1.CurrentPoint property?
>
> So I did this:
>
> function axes1_ButtonDownFcn(hObject, eventdata, handles)
> disp('you clicked!')
> disp(handles.axes1.CurrentPoint)
>
>
> but it never gets called when I click on the plot!?

http://www.mathworks.com/support/solutions/data/1-1B03X.html?solution=1-1B03X

You might also be running into this, if you set your ButtonDownFcn before
calling IMSHOW:

http://www.mathworks.com/support/solutions/data/1-168SX.html?solution=1-168SX

--
Steve Lord
slord@mathworks.com


Subject: Axes tools in GUI

From: David

Date: 1 Jul, 2008 17:25:05

Message: 5 of 15

I'm not getting the "Invalid object handle" error.

I set the 'HitTest' property of the things in the figure to
'off'.

p = imshow(all my stuff);
set(p, 'HitTest', 'off');
cb = colorbar;
set(cb, 'HitTest', 'off');

but the axes1_ButtonDownFcm that guide made still does not
get called.

I then tried setting the WindowButtonDownFcn. I had a bit
more success. It was actually called, but then I have to
manually handle whether or not the click was inside or
outside the plot. This seems to be more of a "hack" way to
do it.

To set the ButtonDownFcn of the object itself, what do I do?
%my_plot_function.m
p = imshow(stuff);
set(p,'ButtonDownFcn', @GetClick(p.CurrentPoint) )

where
GetClick.m is simply

function GetClick(MyPoint)
disp(MyPoint)

It says first "Invalid syntax" on the ( before
p.CurrentPoint. It also says "Too many input arguments
using GetClick".

What am I doing wrong here?

Thanks for the help!

Dave

Subject: Axes tools in GUI

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 1 Jul, 2008 19:49:29

Message: 6 of 15

In article <g4dp9g$l9p$1@fred.mathworks.com>,
David <daviddoria@gmail.com> wrote:

>To set the ButtonDownFcn of the object itself, what do I do?
>%my_plot_function.m
>p = imshow(stuff);
>set(p,'ButtonDownFcn', @GetClick(p.CurrentPoint) )

>where
>GetClick.m is simply

>function GetClick(MyPoint)
>disp(MyPoint)

>It says first "Invalid syntax" on the ( before
>p.CurrentPoint. It also says "Too many input arguments
>using GetClick".

set(p,'ButtonDownFcn', @GetClick);


function GetClick(src,evt)
  disp(get(src,'CurrentPoint'))
end


--
   "Beware of bugs in the above code; I have only proved it correct,
   not tried it." -- Donald Knuth

Subject: Axes tools in GUI

From: David

Date: 1 Jul, 2008 20:09:02

Message: 7 of 15

This is what I never understood about function handles.
Where did you "pass" the values of src and evt to the function??

Subject: Axes tools in GUI

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 1 Jul, 2008 20:18:23

Message: 8 of 15

In article <g4e2su$iu2$1@fred.mathworks.com>,
David <daviddoria@gmail.com> wrote:
>This is what I never understood about function handles.
>Where did you "pass" the values of src and evt to the function??

You don't, not unless you call the function handle yourself.
Remember, this is a callback that is being created: when
Matlab calls a callback that is a function handle, it automatically
supplies the source and event parameters.
--
  "The shallow murmur, but the deep are dumb." -- Sir Walter Raleigh

Subject: Axes tools in GUI

From: David

Date: 1 Jul, 2008 20:42:02

Message: 9 of 15

oh, ok -

what if I wanted to send something else though besides the
src and evt? Like some parameter that I wish to have the
value "5". Is there a way to call GetPoint(src, evt, 5)?

Subject: Axes tools in GUI

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 1 Jul, 2008 21:15:29

Message: 10 of 15

In article <g4e4qq$lan$1@fred.mathworks.com>,
David <daviddoria@gmail.com> wrote:
>oh, ok -

>what if I wanted to send something else though besides the
>src and evt? Like some parameter that I wish to have the
>value "5". Is there a way to call GetPoint(src, evt, 5)?


set(handle,appropriateCallbackName,{@GetPoint,5});

Matlab will see that the callback is a cell array
and will do roughly the equivilent of

cb = get(handle,appropriateCallBackName);
cb{1}(src,evt,cb{2:end});

Except that first it will set the internal variables for
gcf, gcbo and the like, and the internal variables for
the figure CurrentPoint and so on. If you don't make use of those
items in your callback, you can call your callback "manually"
passing handle as the src and passing an appropriate structure
or [] for evt.
--
  "It is surprising what a man can do when he has to, and how
  little most men will do when they don't have to."
                                              -- Walter Linn

Subject: Axes tools in GUI

From: Steven Lord

Date: 1 Jul, 2008 21:22:44

Message: 11 of 15


"David " <daviddoria@gmail.com> wrote in message
news:g4dp9g$l9p$1@fred.mathworks.com...
> I'm not getting the "Invalid object handle" error.
>
> I set the 'HitTest' property of the things in the figure to
> 'off'.
>

I assume you set the axes ButtonDownFcn here, before the IMSHOW call?

> p = imshow(all my stuff);
> set(p, 'HitTest', 'off');
> cb = colorbar;
> set(cb, 'HitTest', 'off');
>
> but the axes1_ButtonDownFcm that guide made still does not
> get called.

The IMSHOW call, because the NextPlot property of the axes was set to the
default value of 'replace', cleared most of the properties of the axes,
including the ButtonDownFcn property. That's the documented behavior of
that property:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/axes_props.html#NextPlot

Look at this:

>> ax = gca;
>> set(ax, 'ButtonDownFcn', @(h, ev) disp('ButtonDownFcn invoked'))

% I clicked the axes twice
ButtonDownFcn invoked
ButtonDownFcn invoked

% I'll display a sample image from Image Processing Toolbox
l>> imshow('eight.tif');

% I clicked the axes several times, no display
>> get(ax, 'ButtonDownFcn')

ans =

     ''

Either set the NextPlot property of your axes to 'replacechildren' before
calling IMSHOW or set your axes ButtonDownFcn after you call IMSHOW.

> I then tried setting the WindowButtonDownFcn. I had a bit
> more success. It was actually called, but then I have to
> manually handle whether or not the click was inside or
> outside the plot. This seems to be more of a "hack" way to
> do it.
>
> To set the ButtonDownFcn of the object itself, what do I do?
> %my_plot_function.m
> p = imshow(stuff);
> set(p,'ButtonDownFcn', @GetClick(p.CurrentPoint) )
>
> where
> GetClick.m is simply
>
> function GetClick(MyPoint)
> disp(MyPoint)
>
> It says first "Invalid syntax" on the ( before
> p.CurrentPoint. It also says "Too many input arguments
> using GetClick".

Correct, that's not valid syntax. You tried using a combination of a
regular function call and a function handle. Instead, you can write your
ButtonDownFcn as an anonymous function wrapper around a call to your
function, if you want its signature to be different from the signature
MATLAB expects for the ButtonDownFcn:

http://www.mathworks.com/access/helpdesk/help/techdoc/ref/axes_props.html#ButtonDownFcn

set(myAxes, 'ButtonDownFcn', @(h, eventdata) GetClick(p.CurrentPoint))

You can passthrough the h and eventdata input arguments with which MATLAB
calls your ButtonDownFcn to the GetClick function, or you can ignore them.
Those input arguments must be accepted by the function that gets called by
MATLAB (in this case, the anonymous function accepts them, then ignores them
and calls GetClick instead.)

For more information on these two arguments, take a look at:

http://www.mathworks.com/access/helpdesk/help/techdoc/creating_plots/f7-55506.html

--
Steve Lord
slord@mathworks.com


Subject: Axes tools in GUI

From: David

Date: 1 Jul, 2008 21:57:01

Message: 12 of 15

great! those posts look like they contain the answers I was
looking for - I'll have to parse through it tomorrow.

Thanks!

Subject: Axes tools in GUI

From: David

Date: 2 Jul, 2008 14:54:02

Message: 13 of 15

Excellent, this syntax is what I was looking for:
set(myAxes, 'ButtonDownFcn', @(h, eventdata)
GetClick(p.CurrentPoint))

It shouldn't be so confusing to do something as simple as this!

For anyone who reads this, just FYI it turns out that imshow
returns an "image" which does not actually have the
CurrentPoint property, so I had to use get(handles.axes1,
'CurrentPoint').

Thanks for all the help!

Subject: Axes tools in GUI

From: David Doria

Date: 18 Jul, 2008 13:33:03

Message: 14 of 15

Someone emailed me asking for clarification, so I thought
I'd post it here:

First I set this:
p = imshow(whatever);
set(p, 'ButtonDownFcn', @(h, eventdata) GetClick(handles) );

I then have this as a nested function in the GUI m file:

function GetClick(handles)
                                                           
                                              
                                                           
                                                           
                                              
CurrentPoint = get(handles.axes1, 'CurrentPoint');
                                                           
                                              
ClickedScan = round(CurrentPoint(1,1));
                                                           
                                              
ClickedCol = round(CurrentPoint(1,2));
                                                           
                                              
                                                           
                                                           
                                              
disp(['Selected Scan: ' num2str(ClickedScan) ' Col: '
num2str(ClickedCol)]);


Seems to do the trick!

Good luck,

Dave

Subject: Axes tools in GUI

From: Sundaresh

Date: 5 Jun, 2009 15:14:03

Message: 15 of 15

"David Doria" <daviddoria@gmail.com> wrote in message <g5q62f$jst$1@fred.mathworks.com>...
> Someone emailed me asking for clarification, so I thought
> I'd post it here:
>
> First I set this:
> p = imshow(whatever);
> set(p, 'ButtonDownFcn', @(h, eventdata) GetClick(handles) );
>
> I then have this as a nested function in the GUI m file:
>
> function GetClick(handles)
>
>
>
>
>
> CurrentPoint = get(handles.axes1, 'CurrentPoint');
>
>
> ClickedScan = round(CurrentPoint(1,1));
>
>
> ClickedCol = round(CurrentPoint(1,2));
>
>
>
>
>
> disp(['Selected Scan: ' num2str(ClickedScan) ' Col: '
> num2str(ClickedCol)]);
>
>
> Seems to do the trick!
>
> Good luck,
>
> Dave

I created a function GetClick.m as

function GetClick(handles)
CurrentPoint = get(handles.axes1, 'CurrentPoint');
ClickedScan = round(CurrentPoint(1,1));
ClickedCol = round(CurrentPoint(1,2));
disp(['Selected Scan: ' num2str(ClickedScan) ' Col: '
num2str(ClickedCol)]);
end
and saved it.

I tried the following code on a normal image which I opened using:

h = imshow('pout.tif');
 set(h, 'ButtonDownFcn', @(h, eventdata) GetClick(handles) );

Now when I clicked on a point in the image I got an error as :


??? Undefined function or variable 'handles'.

Error in ==> @(p,eventdata)GetClick(handles)


??? Error while evaluating image ButtonDownFcn

What is the mistake that I am making?

Thanks,
Ram

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
axes Sprinceana 22 Jun, 2009 07:52:16
gui Sprinceana 22 Jun, 2009 07:52:16
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com