MATLAB: How to create a TEXT Handle Graphics object that I can move in a stand-alone application

applicationcreategraphicshandlehgMATLAB Compilerobjectsplotstand-alonestandalonetext;tool

Text objects created in stand-alone application cannot be moved, because the edit plot tool is not available.

Best Answer

Since the Edit plot tool is not available in stand-alone mode, you must manually write a MATLAB function that implements the capability to move the text in an axis.
function testMove
fig = figure;
ax = axes;
txt = gtext('This text is movable');
set(txt,'UserData', false);
set(txt,'Units',get(fig,'Units'));
set(txt,'ButtonDownFcn',{@activateMove, fig});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


function activateMove(hobj, eventdata, fig)
set(fig,'WindowButtonMotionFcn',{@moveTxtPosition, hobj});
set(fig,'WindowButtonDownFcn',{@openTxtPosition, hobj});
set(fig,'WindowButtonUpFcn',{@closeTxtPosition});
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function openTxtPosition(fig, eventdata, hobj)
set(fig,'WindowButtonMotionFcn',{@moveTxtPosition, hobj})
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function moveTxtPosition(fig, eventdata, hobj)
prevPos = get(fig,'UserData');
if isempty(prevPos)
set(fig,'UserData',get(fig,'CurrentPoint'));
return;
end
curPos = get(fig,'CurrentPoint');
posChange = curPos - prevPos;
txtPos = get(hobj,'pos');
set(hobj,'pos', [posChange + txtPos(1:2), 0]);
set(fig,'UserData',curPos);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function closeTxtPosition(fig, eventdata, hobj)
set(fig, 'WindowButtonMotionFcn','');
set(fig, 'WindowButtonDownFcn','');
set(fig, 'WindowButtonUpFcn','');