Function to copy to clickboard in App Designer?

I am making an application that outputs results in Edit Fields (both text and numeric). I would like to add a function that automatically copies the contents of an Edit Field to the clipboard when the Edit Field is selected (clicked). However, I do not see a callback function for Edit Fields like "ClickedFcn" for Drop Down lists.
Is it possible to do this directly or perhaps via some workaround?

2 commentaires

dpb
dpb le 20 Juil 2025
I've never gotten fancy enough to use them, but the workaround I can think of would be to put the control in a panel that does have the callback. How well it would suit your appication, I don't know...
The best solution I've come up with is to add a button next to the Edit Field and use the ButtonPushedFcn callback with the clipboard function.
function ButtonPushedFcn(app, event)
clipboard('copy',value)
end
It's a good solution but it would be nice to not have to eat up screen real estate.

Connectez-vous pour commenter.

 Réponse acceptée

Matt J
Matt J le 20 Juil 2025
Modifié(e) : Matt J le 20 Juil 2025
Implement a WindowButtonDownFcn callback for the UIFigure canvas,
function CanvasButtonDown(app, event)
clickedComponent = app.UIFigure.CurrentObject;
if clickedComponent == app.MyEditField
% This code runs when the edit field is clicked
disp('Edit field was clicked');
clipboard('copy' , app.MyEditField.Value);
end
end

3 commentaires

Michael P
Michael P le 21 Juil 2025
Modifié(e) : Michael P le 21 Juil 2025
Thank you. This seems to be very close, but I can't quite get it to work. Where should the function be added? I don't see any option for a CanvasButtonDown callback, so I tried adding the code as a UIFigureButtonDown callback:
function UIFigureButtonDown(app, event)
clickedComponent = app.UIFigure.CurrentObject;
if clickedComponent == app.EditField
clipboard('copy', app.EditField.Value);
end
end
Unfortunately, the program never enters the callback at all no matter what I click.
There seems to be an issue because the Edit FIeld in question is inside of a Grid Layout, which is itself inside of a Tab, which is in turn inside of a Tab Group.
The structure is thus app.UIFigure > app.TabGroup > app.MainTab > app.GridLayout > app.EditField.
Is this a poor/incorrect way of structuring an app?
Matt J
Matt J le 21 Juil 2025
Modifié(e) : Matt J le 21 Juil 2025
I don't see any option for a CanvasButtonDown callback
Youi want to set the WindowButtonDownFcn callback in the Component browser (see below). 'CanvasButtonDown' is just a hypothetical name I assigned for the function signature. The name appearing in the function signature of a callback can be anything you want.
There seems to be an issue because the Edit FIeld in question is inside of a Grid Layout, which is itself inside of a Tab, which is in turn inside of a Tab Group.
Doesn't seem to be an issue for me. I've attached a minimal working example app to illustrate.
Michael P
Michael P le 21 Juil 2025
This worked! Thank you for the clear explanation.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur MATLAB Parallel Server dans Centre d'aide et File Exchange

Produits

Version

R2025a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by