Ben Fossen
Ben Fossen

Reputation: 1007

ButtonDownFcn with an argument in Matlab

I have a patch which I have included below and I want to add an additional ButtonDownFcn to the patch. When I do the second option the code does not work I get to many output arguments.

Original patch

for i = 1:10
    counter = counter+1;
    hpatch(counter) = patch([x(17) x(18) x(1) x(1)],[y(17) y(18) y(1) y(1)],[0 0 0 0],...
    'Parent',hAx,'ButtonDownFcn', ['winopen(''' file(counter) ''');']));
end

New desired patch

for i = 1:10
    counter = counter+1;
    hpatch(counter) = patch([x(17) x(18) x(1) x(1)],[y(17) y(18) y(1) y(1)],[0 0 0 0],...
    'Parent',hAx,'ButtonDownFcn', ['winopen(''' file(counter) ''');'],...
    'ButtonDownFcn', @saveClickData(counter));
end

Here is the function

saveClickData(counter)

should it be this?:

saveClickData(source,event)

Upvotes: 0

Views: 1546

Answers (1)

Amro
Amro

Reputation: 124543

Try:

patch(X,Y,Z, 'ButtonDownFcn',{@saveClickData, counter})

Then define the callback function as:

function saveClickData(src,evt,counter)
    winopen( file(counter) );

    %# do additional stuff..
end

Make sure to define this function as a nested function, so that it has access to its parent function workspace including the file variable.

Upvotes: 1

Related Questions