Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to make text in a matlab GUIDE app selectable (and possible to copy) but not editable?

Like question in title says. I would like a text field that should be possible to select just like text you would in a browser, but not editable.

I tried setting the Enable property to off and inactive. I also tried using both text fields and edit texts and static text boxes. Edit text boxes with Enable set to on allows me to select it, but not copy it. I don't know if there could be OS specific issues (Ubuntu 16.04) since matlab seems to like to use other key shortcuts for copy paste in Linux. Funny enough, when I right-click the selectable text, nothing happens.

It should work in GUIDE, so I imagine there is just some setting in properties manager I missed.

like image 806
user1661303 Avatar asked Dec 05 '25 16:12

user1661303


1 Answers

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:

demo gif

like image 194
Hoki Avatar answered Dec 08 '25 08:12

Hoki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!