How to access the system clipboard in MATLAB Web App Server

15 ビュー (過去 30 日間)
Kaiwai
Kaiwai 2024 年 4 月 10 日
回答済み: Rushikesh 2024 年 8 月 16 日
I am creating an app in MATLAB Web App Server 2024a. Accessing the system's clipboard using the clipboard function is not supported, as described in the unsupported functionality page. I am looking for an alternative so that users can paste the contents of their clipboard into a UITable.
Interestingly, despite the lack of support for the clipboard function, users are still able to paste text from their clipboard using the key combination to paste if they double click a cell in the table first so that there's a flashing cursor. This leads me to believe that Web App Server is able to access the system clipboard, although I can't seem to figure out how to do so (ie. if there's a different function to call). Is there a way for me to access the user's clipboard in Web App Server without the clipboard function? As it stands, users can only paste if they've double-clicked a cell in the table. A single click won't do, and I'd like to add functionality to enable that.
Any insights are appreciated. Thank you!
  1 件のコメント
Piyush Kumar
Piyush Kumar 2024 年 4 月 24 日
Hi Kaiwai,
I also faced the same issue. Unfortunately could not find any other function to enable copy from system's clipboard to WebApp UITable. I think it's not possible for now.

サインインしてコメントする。

回答 (1 件)

Rushikesh
Rushikesh 2024 年 8 月 16 日
Hello @Kaiwai,
I understand that you want to programmatically copy systems clipboard data in UITable inside MATLAB Server Web App. Yes, MATLAB does not support “clipboard” function in Web Server App. However, there is an alternate workaround to copy from system clipboard to “UITable” in MATLAB Web App Server using JAVASCRIPT integration.
You can use JAVASCRIPT through HTML Component inside App Designer. Idea is to get system clipboard data inside JAVASCRIPT and access it from MATLAB Web App by using MATLAB DataChange Callback on HTML Component. You can refer to below steps to get generalized solution.
Assuming that you already have “UITablecomponent with some pre-populated data inside App.
Step 1: Embed Custom HTML Component
  1. Locate HTML Component:In App Designer, find the HTML Component in the left-side components section.
  2. Drag and Drop:Drag and drop the HTML Component into your App.
  3. Create HTML File:
  • Name the file js_clipboard.html.
  • Store it in the same folder as your App for convenience.
4. Set HTML Source:In the HTML Component properties, set the HTMLSource value to the path of the js_clipboard.html file.
Step 2: Configure the HTML File
  1. Create a Button:In the js_clipboard.html file, create a simple button, for example, labeled "paste".
  2. Add Event Listeners:
  • Use a <script> tag to write an eventListener for the "paste" event.
  • Store the system clipboard data in a local variable.
  1. Handle Click Event:
  • Add a click eventListener to the button.
  • Assign the local variable value to htmlComponent.Data, which is accessed by the MATLAB App.
Example of HTML file can be shown as:
<!DOCTYPE html>
<html>
<head>
<title>Clipboard Handler</title>
</head>
<body>
<button id="nextButton">paste</button>
<script>
let clipboardData = "default";
function setup(htmlComponent) {
let btn = document.getElementById("nextButton");
btn.addEventListener("click", function(event) {
htmlComponent.Data = clipboardData;
});
}
document.addEventListener('DOMContentLoaded', (event) => {
document.addEventListener('paste', (event) => {
clipboardData = event.clipboardData.getData('text');
});
});
</script>
</body>
</html>
Step 3: Integrate HTML and MATLAB App Components
  1. Setup in MATLAB App:
  • Assume you have app.UIFigure as the parent component and app.HTML and app.UITable as child components.
2. Attach startupFcn Callback:
  • Add a startupFcn callback to the UIFigure component.
  • Inside this function, configure the HTMLSource property of app.HTML to point to the HTML file path.
  • Assign a custom function handler (e.g., handlePaste) to app.HTML.DataChangedFcn.
3. Define the handlePaste Function:
  • This function will assign event.Data to a cell in the UITable.
Sample code of MATLAB Web App looks like this:
methods (Access = private)
function handlePaste(app, ~, event)
clipboardData = event.Data;
% Assuming you have a UITable named 'UITable'
% Get the selected cell and paste the clipboard data
app.UITable.Data{1, 1} = clipboardData; % Example: Pasting into the first cell
disp(clipboardData);
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.UITable.Data = {1,1,1,1; 2,2,2,2; 3,3,3,3};
app.HTML.HTMLSource = fullfile(pwd, 'js_clipboard.html');
% app.HTML.Data = "Hello world";
% Set up the listener for JavaScript events
app.HTML.DataChangedFcn = @app.handlePaste;
% addlistener(app.HTML, 'DataChanged', @app.handlePaste);
end
end
Please refer to the below documentation for the detailed instructions on JAVASCRIPT integration.
Note: This workaround allows you to programmatically access the system clipboard. However, it requires using Ctrl+V in the HTML component window, followed by clicking the "paste" button to transfer clipboard data to a UITable cell. You can optimize the code based on your specific requirements.
Hope this helps.

カテゴリ

Help Center および File ExchangeMATLAB Web App Server についてさらに検索

製品


リリース

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by