Passing Matlab's Variable To JavaScript via COM

조회 수: 3 (최근 30일)
Wan Nawi
Wan Nawi 2011년 5월 25일
Hello,
I would like to ask a question (Isn't that obvious?).
I have MATLAB as COM-Client and InternetExplorer as COM-Server.
I want to invoke a JavaScript method using variables from MATLAB. To invoke the method, I use the following command. (h is the handle for COM)
h.Document.parentWindow.execScript('JavaScriptFunction(22,22,22);','JavaScript');
I would like to change the number 22 with MATLAB variables. The problem is, the substitution of the 22 with a variable does not work as the variable is between the apostrophes.
I tried using this way to invoke the method.
h.Document.parentWindow.execScript(['''JavaScriptFuntion(' num2str(variableone) ',' num2str(variabletwo) ',' num2str(variablethree) ')'',''JavaScript''']);
But no luck. Following error occurs:
??? Invoke Error, Dispatch Exception:
Description: Type mismatch.
Help File: C:\Windows\System32\mshtml.hlp
Help Context ID: 0
My idea to solve this problem is to pass the variables to JavaScript and then invoke the method using that variables. But, I don't know how.
Any help?

채택된 답변

Kaustubha Govind
Kaustubha Govind 2011년 5월 25일
It seems like the method execScript requires two arguments, but in your second statement, you are effectively passing in only one string. Also, you are concatenating unnecessary apostrophes. What happens when you try:
h.Document.parentWindow.execScript(['JavaScriptFuntion(' num2str(variableone) ',' num2str(variabletwo) ',' num2str(variablethree) ')'], 'JavaScript');

추가 답변 (1개)

Wan Nawi
Wan Nawi 2011년 5월 25일
Thanks! Your solution works!
Btw, with the second statement I would like to reproduce the string needed to execute the function:
'JavaScriptFunction(22,22,22);','JavaScript'
But it looks like that the execScript method does not really want the to be executed function stays between apostrophes.
Regarding the other question, is the variable passing between matlab and an HTML document containing JavaScript possible?
Regards, Wan
  댓글 수: 1
Kaustubha Govind
Kaustubha Govind 2011년 5월 26일
Yes. In this case, MATLAB is acting like a COM client. The documentation describes handling of COM Data in MATLAB: http://www.mathworks.com/help/techdoc/matlab_external/bq4epvg-1.html

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Write COM Applications to Work with MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by