Display my data from MATLAB to uiHTML

16 次查看(过去 30 天)
John Cedrick
John Cedrick 2021-11-15
回答: Sameer 2024-11-14,7:05
Hello, I'm new in MATLAB and uihtml, i created html(this is just practice codes) then it display in command window of MATLAB, may I ask if how can I display my data from to HTML? Here's my codes:
**MATLAB**
figure = uifigure;
figure.Position = [500 500 490 180];
HTML = uihtml(figure);
HTML.Position = [20 20 450 130];
HTML.HTMLSource = fullfile(pwd, 'html.html');
HTML.DataChangedFcn = @(src, event) dataChangedCallback(src, event);
function dataChangedCallback(~, event)
HTML = event.Data;
disp(HTML.Name)
end
**HTML**
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<script type="text/javascript" >
function setup(htmlComponent) {
document.getElementById('myForm').addEventListener('submit', function (event){
htmlComponent.Data = {'Name': document.getElementById('inputName').value};
})
htmlComponent.addEventListener("HTML", function(event) {
document.getElementById("dataDisplay").innerHTML = htmlComponent.Data;
});
}
</script>
</head>
<body>
<form id="myForm">
<label for="Name">Input your name:</label>
<input type="text" id="inputName">
<button type="submit" id="submit" >Submit</button>
<br>
<div id="dataDisplay">
Display data here..
</div>
</form>
</body>
</html>

回答(1 个)

Sameer
Sameer 2024-11-14,7:05
To display data from your HTML form in MATLAB using the "uihtml" component, you need to ensure that the data is correctly passed from the HTML form to MATLAB and then handled appropriately.
There are a few issues in your current setup that need to be addressed:
1. You need to ensure that the data is correctly passed from the HTML to the MATLAB environment. Your JavaScript code should correctly format the data and trigger the "DataChangedFcn" callback in MATLAB.
2. When passing data from JavaScript to MATLAB, ensure it's in a format MATLAB can understand, such as JSON.
Here's how you can modify your code:
MATLAB Code
% Create a UI figure
fig = uifigure;
fig.Position = [500 500 490 180];
% Create a uihtml component
htmlComponent = uihtml(fig);
htmlComponent.Position = [20 20 450 130];
htmlComponent.HTMLSource = fullfile(pwd, 'html.html');
% Define the data changed callback function
htmlComponent.DataChangedFcn = @(src, event) dataChangedCallback(src, event);
function dataChangedCallback(~, event)
% Extract the data sent from HTML
data = event.Data;
if isfield(data, 'Name')
disp(['Name: ', data.Name]); % Display the name in the command window
end
end
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<script type="text/javascript">
function setup(htmlComponent) {
document.getElementById('myForm').addEventListener('submit', function (event) {
event.preventDefault(); // Prevent the default form submission
// Get the input value
var name = document.getElementById('inputName').value;
// Send data to MATLAB
htmlComponent.Data = {Name: name};
// Display the data within the HTML
document.getElementById("dataDisplay").innerHTML = 'Name: ' + name;
});
}
</script>
</head>
<body>
<form id="myForm">
<label for="Name">Input your name:</label>
<input type="text" id="inputName">
<button type="submit" id="submit">Submit</button>
<br>
<div id="dataDisplay">
Display data here..
</div>
</form>
</body>
</html>
Hope this helps!

类别

Help CenterFile Exchange 中查找有关 Develop uifigure-Based Apps 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by