2

let's assume the following easy example:

f = figure;
plot(-10:10, (-10:10).^3, '*-r');
x = 1;
y = 1;
set(f, 'ResizeFcn', {@resizeCallback2, x, y});

while 1
    [x, y, button] = ginput(1);
    if(button ~= 1)
        break;
    end

    set(f, 'ResizeFcn', {@resizeCallback2, x, y});
end

%%--------------------------
function resizeCallback2(hFig, ~, foo, bar)    
    foo
    bar
end

Is there any easier way to always pass the ACTUAL* values for x and y to the callback function instead of having to always update it in the loop? Thanks!

1
  • Similar to my question Commented Mar 25, 2014 at 5:05

2 Answers 2

4

It looks like you are trying to store the value of a mouse click position, and then use those values as a part of the resize function (which would be called at a later time). There are a few changes that I would make.

First, instead of the while loop, use another callback to capture the mouse click. For example, you could use the figure ButtonDownFcn call back to trigger a function which was designed to capture the mouse position into some location.

Second, there are better ways to store the mouse position, and the right way will depend on your skill level and the needs of your program. Some of these methods of storing data are:

  1. In the arguments to another callback, like you are doing now. This is pretty painful, but it probably works. So you could keep it if it is good enough for your needs.

  2. The 'userdata' field in most Matlab objects. A few people have brought this up, and it will work fine. I don't like to rely on this because I'm always afraid that some other tool will also want to use the userdata field, and the tools will overwrite data.

  3. A global variable value. I don't like to use globals either, for the same reason I don;t like to use the userdata field. But globals are sometimes the best solution anyway. This is probably the easiest, lowest effort solution to your problem if you only have one figure at a time. (Multiple figures would drive you towards the userdata solution as the easiest solution.)

  4. Provide a handle class to store some data (i.e. x and y) and give a copy of that class to each of the two callbacks (ButtonDownFcn and ResizeFcn). This allows the two functions to pass data, without polluting anyone else's namespace. This is my favorite solution to this sort of problem, so I'll give it a more detailed description below.


To execute option (4) above would need a class to store the data that looks something like this:

    classdef ApplicationData < handle
        properties (SetAccess = public, GetAccess = public)
            x = [];
            y = [];
        end
    end

Note that since ApplicationData extends handle, Matlab treats it as a pass-by-reference object, which is useful to us.

Then you can create an instance of this class, and give it to each callback function.

    dataPassing = ApplicationData;
    set(f, 'ButtonDownFcn', @(x,y) mouseClickCapture(x,y,dataPassing));
    set(f, 'ResizeFcn',     @(x,y) resizeCallback2(x,y, dataPassing));

Where mouseClickCapture looks something like this:

    function mouseClickCapture(hAxis, ignored, dataPassingClass)
    mousePositionData = get(hAxis,'CurrentPoint');
    dataPassingClass.x = mousePositionData(1,1);
    dataPassingClass.y = mousePositionData(1,2);

And your resizeCallback2 looks something like this:

    function resizeCallback2(h, ignored, dataPassingClass)
    %Do something here using 
    %dataPassingClass.x
    %and
    %dataPassingClass.y
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks, I really like your approach. I especially like it because the thing I'm trying to do actually is a problem which is somehow related to 'OOP': What I mean is that I have about 10 vars which I need to pain from function to function which all relate to one 'object', thus creating some sort of object and using its attributes is a lovely idea :) Thanks for it! Further I like your approach to NOT use ginput: I already had a problem with it when I wanted to somehow abort ginput() from another function when a certain action happened and I couldn't since there was no actual click!
There's just one problem probably, which I found in matlab newsreader: Handles classes are probably slow: mathworks.com/matlabcentral/newsreader/view_thread/…
I'd be curious to hear if you found the same slow performance in your application. I have two reasons to suspect that you will not. First, that post was written using 2009a, and the Mathworks usually fixes performance issues like that after 1-2 versions. Second, the strangest thing in that post was that similar operations in handle classes were very different, not that handle classes were uniformly slow. Knowing that (strange as it seems) you could probably work around any speed issue. That said, I've never used a handle class where performance was a big issue, so I just don't know.
So far I haven't, but I haven't really put 'heavy' calculations into handle classes since there was no need to yet :) But again, I have to say thanks,I 'm using this solution for some weeks now and I really really like it :) Very elegant and smooth!
3

I'm not sure what you're actually trying to do - perhaps your simple example has obscured your real intentions - but rather than repeatedly setting a new version of the ResizeFcn, could you just store something in the UserData property of the figure, and have your ResizeFcn read that when it's executed?

3 Comments

I agree, the standard pattern is to use the UserData. It's much clearer than changing the ResizeFcn.
Thanks, didn't know there's such a field to store things... But still I would have to update the values of the UserData in each iteration.
That's right - why is that a problem? What are you really trying to do ultimately, rather than the simple example?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.