How to integrate HTML into Matlab App Designer?

17 views (last 30 days)
Alex Clonan
Alex Clonan on 20 Oct 2021
Answered: Kojiro Saito on 21 Oct 2021
Hello all!
As a quick preface I am using MatLab 2021a.
I have been in the process of making a GUI for an auditory neuroscience project, and was trying to use an HTML UI component for the App Designer, since I am pretty familiar with HTML. However, I have been struggling to add MatLab functionality to my setup. Currently, I have the HTML mocked up, however don't understand how to integrate callbacks, the properties or the methods. I wasn't able to find much documentation online.
Currently for the callback I have:
function HTMLDataChanged(app, event)
latestvalue = event.Value;
app.HTML.Value = latestvalue;
end
I was not sure how to connect this to any variables within my HTML code; I was trying something along the following for an initial property, I was getting a syntax error, but I haven't been able to find the syntax online.
properties (Access = private)
app.HTML.HearingStatus.Value = 'NH';
end
If anyone could point me in the right direction it would be greatly appreciated.
Thank you so much for your help!

Answers (1)

Kojiro Saito
Kojiro Saito on 21 Oct 2021
In uihtml document, it says,
To synchronize the value of the Data property between MATLAB and the third-party content that you are embedding in your app, create a setup function in an HTML file that connects a JavaScript object called htmlComponent to the HTML UI component in MATLAB. Then, set the HTMLSource property value to the path to the file.
So, you need to create setup function in HTML/JavaScript to connect HTML and MATLAB.
I've attached mlapp and two html files. myHtml.html is a sample for passing data from MATLAB to HTML and myHtml2.html is one which passes data from HTML to MATLAB.
myHtml.html (sample for passing data from MATLAB to HTML)
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
function setup(htmlComponent) {
htmlComponent.addEventListener("DataChanged", function(event) {
document.getElementById("dataDisplay").innerHTML = htmlComponent.Data;
});
}
</script>
</head>
<body>
<div id="prompt">
<span><label for="prompt"><strong>Data from MATLAB will display here:</strong></label></span>
<br/><br/>
<div id ="dataDisplay">
Please set data in MATLAB...
</div>
</div>
</body>
</html>
myHtml2.html (sample for passing data from HTML to MATLAB)
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<div id="textInput">
<span><label for="textInput"><strong>Data to MATLAB:</strong></label></span>
<br/><br/>
<div >
Input <input id="dataInput" type="text" placeholder="Type something"></input>
</div>
</div>
</body>
<script type="text/javascript">
function setup(htmlComponent) {
const input = document.getElementById('dataInput');
//const html = document.querySelector('html');
input.addEventListener('input', updateValue);
function updateValue(e) {
htmlComponent.Data = e.target.value;
}
}
</script>
</html>
Demo
Please let me know if you have further question.

Community Treasure Hunt

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

Start Hunting!