« Earlier3 items total Later »

Adding drag capabilities to a Matlab gui

Here is an example from the newsgroup

but could be probably done neater with nested functions.

function test
 % test gui function

 % create figure
 fig = figure;

 % set the figure's WindowButtonDownFcn
 set(fig,'WindowButtonDownFcn',{@wbd});
end


function wbd(h,evd)

 disp('down')

 % get the values and store them in the figure's appdata
 props.WindowButtonMotionFcn = get(h,'WindowButtonMotionFcn');
 props.WindowButtonUpFcn = get(h,'WindowButtonUpFcn');
 setappdata(h,'TestGuiCallbacks',props);

 % set the new values for the WindowButtonMotionFcn and
 % WindowButtonUpFcn
 set(h,'WindowButtonMotionFcn',{@wbm})
 set(h,'WindowButtonUpFcn',{@wbu})
end


function wbm(h,evd)
 % executes while the mouse moves

 disp('motion')
end


function wbu(h,evd)
 % executes when the mouse button is released

 disp('up')

 % get the properties and restore them
 props = getappdata(h,'TestGuiCallbacks');
 set(h,props);
end

Input data using a mouse

Use ginput function:

 x=0;y=0;
 while ~isempty(x)
    [x1,y1]=ginput(1);
    plot([x x1],[y y1],'b.-');
    hold on
    x=x1;y=y1;
end

Adding Callbacks to uitable

The basic uitable object in matlab is just a peer for the true table. To expose all the possible callbacks do the following


>> t = uitable;
>> tj = t.getTable;
>> set(tj)



gives you



ComponentHiddenCallback: string -or- function handle -or- cell array
ComponentMovedCallback: string -or- function handle -or- cell array
ComponentResizedCallback: string -or- function handle -or- cell array
ComponentShownCallback: string -or- function handle -or- cell array
MouseDraggedCallback: string -or- function handle -or- cell array
MouseMovedCallback: string -or- function handle -or- cell array
PropertyChangeCallback: string -or- function handle -or- cell array
FocusGainedCallback: string -or- function handle -or- cell array
FocusLostCallback: string -or- function handle -or- cell array
CaretPositionChangedCallback: string -or- function handle -or- cell array
InputMethodTextChangedCallback: string -or- function handle -or- cell array
KeyPressedCallback: string -or- function handle -or- cell array
KeyReleasedCallback: string -or- function handle -or- cell array
KeyTypedCallback: string -or- function handle -or- cell array
MouseClickedCallback: string -or- function handle -or- cell array
MouseEnteredCallback: string -or- function handle -or- cell array
MouseExitedCallback: string -or- function handle -or- cell array
MousePressedCallback: string -or- function handle -or- cell array
MouseReleasedCallback: string -or- function handle -or- cell array
HierarchyChangedCallback: string -or- function handle -or- cell array
VetoableChangeCallback: string -or- function handle -or- cell array
ComponentAddedCallback: string -or- function handle -or- cell array
ComponentRemovedCallback: string -or- function handle -or- cell array
AncestorMovedCallback: string -or- function handle -or- cell array
AncestorResizedCallback: string -or- function handle -or- cell array
AncestorMovedCallback_: string -or- function handle -or- cell array
AncestorAddedCallback: string -or- function handle -or- cell array
AncestorRemovedCallback: string -or- function handle -or- cell array
MouseWheelMovedCallback: string -or- function handle -or- cell array
ButtonDownFcn: string -or- function handle -or- cell array



You add the callback like

set(tj,'MouseClickedCallback',@(handle,event)disp('hello world'));


The handle is argument to the callback is the handle to the uitable and the event is a subclass of java.awt.event.

« Earlier3 items total Later »




Sponsored by

Sole Central

Your one stop shop for Birkenstock and Crocs shoes and sandles.