There is a way to achieve that but (i) not directly in GUIDE, and (ii) not even in pure MATLAB. This solution relies on undocumented underlying java properties of the MATLAB edit
box.
To be able to access these properties, you first need to download the finjobj
utility from the file exchange.
Armed with that, the following code works fine on MATLAB R2015a, you may have to adjust or fiddle for other MATLAB versions.
Code for demo_editbox_interception.m
:
function h = demo_editbox_interception
% create minimalistic text box
h.fig = figure('Toolbar','none','Menubar','none') ;
h.ht = uicontrol('style','edit','Position',[20 20 300 30],...
'String','This is a multi-word test string');
% find the handle of the underlying java object
h.jt = findjobj(h.ht) ;
% disable edition of the java text box
h.jt.Editable = 0 ;
% => This leaves you with a text box where you can select all or part of
% the text present, but you cannot modify the text.
%% Now add copy functionality:
% choose which functionality you want from the options below:
% set(h.ht,'ButtonDownFcn',@copy_full_string)
set(h.ht,'ButtonDownFcn',{@copy_selection,h.jt} )
function str = copy_full_string(hobj,~)
% this function will copy the ENTIRE content of the edit box into the
% clipboard. It does NOT rely on the undocumented java properties
str = get(hobj,'String') ;
clipboard('copy',str)
disp(['String: "' str '" copied into the clipboard.'] )
function str = copy_selection(hobj,evt,jt)
% this function will copy the SELECTED content of the edit box into the
% clipboard. It DOES rely on the undocumented java properties
str = jt.SelectedText ;
clipboard('copy',str)
disp(['String: "' str '" copied into the clipboard.'] )
You can see it in effect. Select text with the left mouse button, and fire the callback (the copy) with the right button:
