Import Data from html-built UI to Matlab
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hello. I've created user interface (a form) using HTML and CSS and embedded it onto matlab using uihtml component. I've successfully loaded my UI through matlab however, I cannot fetch the data entered from the UI. I need those data and input them in my anfis model which is generated through NeuroFuzzy Designer here in matlab. After the data processed through the model, i then need to display the result onto my UI. Main goal of my project is to predict risk level of patients in getting pneumonia. Question is, how can I fetch data from my UI and pass it to matlab, and vice versa?
採用された回答
Antonio Hortal
2021 年 11 月 15 日
Hi Jovelyn, I can recommend you to go through this article that explains how the "Data" property in the uihtml component lets you connect your Matlab code with your HTML one: "Create HTML File that can trigger and respond to data changes". The documentation for the uihtml object also has some nice examples on how to set up the connection between Matlab and HTML
For your form you could try something like this:
- In your html file, create a setup function (like in the article) that appends a submit listener to your form element. When registering a submit event, you will need to change the "Data" property of the htmlcomponent to whatever you want your Matlab code to see.
function setup(htmlComponent) {
document.getElementById('yourFormId').addEventListener('submit', function (event){
htmlComponent.Data = {'Name': document.getElementById('yourFormInput').value};
})
}
2. In Matlab, add a 'DataChangedFcn' callback to your uihtml element. The second argument to the callback (the event) contains the "Data" that was sent from the HTML file
yourUIHTML.DataChangedFcn = @(src, event) dataChangedCallback(src, event);
function dataChangedCallback(src, evt)
yourHTMLData = evt.Data;
disp(yourHTMLData.Name)
end
10 件のコメント
Jovelyn Obias
2021 年 11 月 15 日
Thank you sir for your response. In html, the setup function must be called upon clicking the button right?
<button onclick = "setup()" type = "submit">SUBMIT</button>
Also, i am getting an error like this 
<script type = "text/javascript">
function setup (htmlComponent){
document.getElementbyId('form_ID').addEventListener('submit',function(event){
htmlComponent.Data = {'Age': document.getElementbyId('age')};
});
}
</script>
that is the js i've written
% Callbacks that handle component events
methods (Access = private)
% Data changed function: questions
function questionsDataChanged(app, event)
data = event.Data;
disp(data.Age);
end
end
and this in matlab
Antonio Hortal
2021 年 11 月 15 日
The button doesn't need to call the setup() function. The setup() function is called internally by Matlab when the html code finishes rendering, and is meant for you to define inside the listeners for the actions that you want to see in Matlab (in this case, you are listening to the 'submit' event on the form)
If your DOM looks like this
<form id="form_ID">
<label>Test field: <input id='age' type="text"></label>
<button type="submit">SUBMIT</button>
</form>
then your code should work. But you need to get the 'value' property of the input:
htmlComponent.Data = {Age: document.getElementbyId('age').value};
(The Age property does not need to go with "")
Jovelyn Obias
2021 年 11 月 15 日
I've already resolved it. Thank you so much. Does the disp function meant to display the data in command window?
Now I'm trying to get the data through getElementsByClassName to get all data in one go. Is that possible?
Jovelyn Obias
2021 年 11 月 15 日
I already tried replacing getElementsById with getElementsByClassName('classNameofallInputs'), but matlab pops up the error (Non existent field 'Age')
I also tried doing getElementsById for every input and it worked. However to make the code simple i want to place all the inputs in an array, if possible.
Antonio Hortal
2021 年 11 月 15 日
編集済み: Antonio Hortal
2021 年 11 月 15 日
Since you are probably not an avid javascript/html programmer (neither I am haha, Matlab is the thing I know best), I would recommend you to use the getElementsById just as you did. It might not be the prettiest piece of code, but if it works, it works :)
Still, if you feel like learning new things of javascript and the DOM, you can read about querySelector() and see if maybe you can improve your code. Or about getElementsByClassName (but read the docs!, you are probably using it incorrectly)
Good luck with your code!
Oh, and if my first answer helped you, you can accept it :)
Jovelyn Obias
2021 年 11 月 16 日
Thank you so much! Yes i would. Thanks!
Jovelyn Obias
2021 年 11 月 16 日
Can I add another question? Now that I passed the data to matlab, i stored the 19 inputs to the riskInput and 5 inputs to the typeInput and processed them through evalfis. But im getting this error

So I enclosed riskInput and typeInput inside the double(), this is my code now
% Data changed function: Assessment
function AssessmentDataChanged(app, event)
data = event.Data;
rfis = readfis('riskFis');
tfis = readfis('typeFis');
riskInput = [data.Age data.Temp data.Lung data.Heart data.Diabetes data.HIV data.Cough data.Chemo data.Smoke data.Alcohol data.Chemical data.Swallow data.Sweating data.Breathe data.Chest data.Appetite data.Fatigue data.Nausea data.Confusion];
riskOutput = evalfis(rfis, double(riskInput));
typeInput = [data.Age data.Temp data.Cough data.Hospital data.Ventilator];
typeOutput = evalfis(tfis, double(typeInput));
disp(riskOutput)
disp(typeOutput)
end
but im having an "Input data must have as many columns as input variables and as many rows as independent sets of input values" error now. I counted my inputs already and the numbers are correct.
Antonio Hortal
2021 年 11 月 16 日
I am sorry, I am not familiar with the Fuzzy Logic Toolbox. Maybe you can open another question to get help
Jovelyn Obias
2021 年 11 月 18 日
Sorry for adding another question, inside the script tag can i add another function for may html functionalities? Cause when i added function, "htmlsource may be referencing unsupported functionality or may have a javascript error" appears just after entering answer in the first question. I think the setup function is being triggered immediately
Antonio Hortal
2021 年 11 月 19 日
You can creare as many functions as you want in your <script> tags inside the HTML file.
The warning message that you get in the command window is thrown when your javascript code throws an error. On this article there is a section called 'Debug your HTML code', give it a try :)
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Fuzzy Logic in Simulink についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
