File Exchange

image thumbnail

Generate JavaScript Using MATLAB Coder

version 2.0.2 (956 KB) by Geoff McVittie
Create JavaScript/WebAssembly apps and libraries from MATLAB projects using MATLAB Coder.

28 Downloads

Updated 24 Oct 2019

View Version History

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

Generate JavaScript using MATLAB Coder Add-On in combination with the Emscripten compiler converts your MATLAB functions into high-performance, client-side JavaScript/WebAssembly apps and libraries. Generated code can be compiled, embedded, and run in any modern browser; including, Google Chrome, Mozilla FireFox, Microsoft Edge, and Apple Safari. The generated code can also be run in standalone JavaScript engines, such as NodeJS.

Cite As

Geoff McVittie (2020). Generate JavaScript Using MATLAB Coder (https://www.mathworks.com/matlabcentral/fileexchange/69973-generate-javascript-using-matlab-coder), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (36)

Andrew McColl

Hi Geoff,
Looks like an interesting Add-On! I'm with a group working on arbitrary distributed computing with JavaScript/Wasm, on any CPU or GPU. I'd like to have a quick chat and see if there is a way we may collaborate.
Feel free to email me at andrew@kingsds.network

Cheers,
Andrew McColl

Geoff McVittie

Hello Yoshinobu,
There is an issue with the toolchain registration in R2020a which may result in the selection of a different toolchain, e.g. MINGW, instead of the Emscripten toolchain. Can you run the Add-On in R2019b release of MATLAB to see if the issue persists?

Thank you,
Geoff McVittie

Yoshinobu Sasaki

Yoshinobu Sasaki

Hell geoff, great works!
I encountered error as foloows when executing 'webcoder.build.project(proj)' .
(OS:windows 10, MATLAB:2020a, compiler:mingw)
------------------------------------------------------------------------
"C:\PROGRA~3\MATLAB\SUPPOR~1\R2020a\3P778C~1.INS\MINGW_~1.INS\bin/g++" -c -fwrapv -m64 -std=c++11 -O0 -D__USE_MINGW_ANSI_STDIO=1 -DMODEL=mynewapp -DMODEL=mynewapp @mynewapp_rtw_comp.rsp -o "main.obj" "C:/Users/[Username]/AppData/Roaming/MathWorks/MATLABAdd-Ons/Toolboxes/GenerateJavaScriptUsingMATLABCoder/src/main.cpp"
C:/Users/[Username]/AppData/Roaming/MathWorks/MATLABAdd-Ons/Toolboxes/GenerateJavaScriptUsingMATLABCoder/src/main.cpp:3:24: fatal error: emscripten.h: No such file or directory
#include <emscripten.h>
^
compilation terminated.
gmake: *** [main.obj] Error 1
The make command returned an error of 2

"mynewapp" のビルド中にエラーが発生しました:
### Failed to generate all binary outputs.
------------------------------------------------------------------------

I already fixed MINGW's space issues, although is happens.
do you think what's wrong?
Thank you for advance.

Nicolas Hinze

I finally fixed the error. After removing the spaces in the folder you need to run the following command in MATLAB before attempting to compile again:
webcoder.setup.createAndRegisterToolchain;

Nicolas Hinze

Geoff, I changed the AddOn directory to:
C:\Users\<username>\AppData\Roaming\MathWorks\MATLABAddOns\Toolboxes\GenerateJavaScriptUsingMATLABCoder
but it still fails with the same error message.
I also messaged you through the contact link. I cannot locate an eMail address anywhere.

Geoff McVittie

Hello Nicolas,
This may be related to the same issue seen by Tom McGrath and Christian Golcic. Currently there are issues with paths containing spaces and network mapped drives that is being addressed for the next update. For the moment, please change the location of the Add-Ons directory to be on a Windows path that does not contain any spaces. You can do this by going to the MATLAB Home tab > Environment > Preferences, set MATLAB > Add-Ons > Installation Folder to a directory that does not contain any spaces; it does appear that the default folder location "C:\Users\<username>\AppData\Roaming\MathWorks\MATLAB Add-Ons" contains a space. This can be changed to "C:\Users\<username>\AppData\Roaming\MathWorks\MATLABAddOns". Please let me know if this resolves the issue. You can also reach out to me via the email included in the downloaded Add-On info available in the Add-On Manager.

Thank you,
Geoff McVittie, PhD.

Nicolas Hinze

I'm getting the following error trying to build the CreateApp on Windows 10 with R2019b 64Bit.

>> proj = webcoder.setup.project("My New App","Directory",pwd);
>> proj = openProject(pwd);
webcoder.build.project(proj);
------------------------------------------------------------------------
Adding directories to PATH:
PATH += D:\GitHub\emsdk
PATH += D:\GitHub\emsdk\node\12.18.1_64bit\bin
PATH += D:\GitHub\emsdk\python\3.7.4-pywin32_64bit
PATH += D:\GitHub\emsdk\java\8.152_64bit\bin
PATH += D:\GitHub\emsdk\upstream\emscripten

Setting environment variables:
EMSDK = D:/GitHub/emsdk
EM_CONFIG = D:\GitHub\emsdk\.emscripten
EMSDK_NODE = D:\GitHub\emsdk\node\12.18.1_64bit\bin\node.exe
EMSDK_PYTHON = D:\GitHub\emsdk\python\3.7.4-pywin32_64bit\python.exe
JAVA_HOME = D:\GitHub\emsdk\java\8.152_64bit
EM_CACHE = D:/GitHub/emsdk/upstream/emscripten\cache
Setting the following tools as active:
node-12.18.1-64bit
python-3.7.4-pywin32-64bit
java-8.152-64bit
releases-upstream-536568644fd67d53778f6111fdd5f64ad3f4c539-64bit

The changes made to environment variables only apply to the currently running shell instance. Use the 'emsdk_env.bat' to re-enter this environment later, or if you'd like to permanently register this environment globally to all users in Windows Registry, rerun this command with the option --global.
gmake: *** No rule to make target `Add-Ons/Toolboxes/Generate', needed by `_log.bc'. Stop.
The make command returned an error of 2

Error(s) encountered while building "mynewapp":
### Failed to generate all binary outputs.
------------------------------------------------------------------------
??? Build error: C++ compiler produced errors. See
the Build Log for further details.
More information
Code generation failed: View Error Report
Error using codegen

Error in webcoder.internal.coder.buildProject

Error in webcoder.build.project (line 18)
webcoder.internal.coder.buildProject(proj);

Christian Golcic

Hello,

I have the same issue as Tom McGrath below (posted 1 Jun 2020). I tried contacting you, Mr. Geoff McVittie, here on MathWorks. I cannot find your email in the downloaded Add-On.

I can be contacted in dic15cgo@student.lu.se

Thanks

Geoff McVittie

Hi Tom,
I am sorry to hear you have encountered this issue. Can you reach out to me via the email included in the downloaded Add-On? I suspect the issue may be related to the installation path, but it will almost certainly be more efficient to communicate via email.

Thank you,
Geoff McVittie

Tom McGrath

Hello Geoff!

I have completed the setup without getting any errors, but I am getting the following error when trying to build the first example app:

------------------------------------------------------------------------
gmake: *** No rule to make target `/src/console/_log.cpp', needed by `_log.obj'. Stop.
The make command returned an error of 2

Error(s) encountered while building "mynewapp":
### Failed to generate all binary outputs.
------------------------------------------------------------------------
??? Build error: C++ compiler produced errors. See the Build Log for further details.
More information
Code generation failed: View Error Report
Error using codegen

Error in webcoder.internal.coder.buildProject

Error in webcoder.build.project (line 18)
webcoder.internal.coder.buildProject(proj);

Any ideas where I might have gone with the installation?

David Fardis

Good ... I like it... Thanks ... For Formatting Javascript I used https://javascriptbeautifier.org/

Geoff McVittie

Hi Tong Lumine,
Please contact me through the contact email provided in the Add-On in the add-on manager. I will be happy to help investigate this issue you have encountered, but it will be easier to communicate via email.
Thank you,
Geoff McVittie

tong lumine

i build my project
>> proj = webcoder.setup.project("My New App");
>> webcoder.build.project(proj);
it occurs
fatal error C1083: cannot open include file : “emscripten.h"

Geoff McVittie

Some users have encountered the following error: "UnicodeDecodeError: 'ascii' codec can't decode byte ". This error results from python2.7 in the Emscripten SDK not being able to recognize non-ASCII characters in the path string on Windows. Simple Chinese characters would be example of non-ASCII characters.
As a workaround, you can install the Emscripten SDK and this Add-On into a local folder that only contains ASCII text.
Special thanks to @zhaozhouwu for discovering and reporting this issue.

Caleb Magruder

zhaozhou wu

Thank you Mr. McVittie.
I followed your advices and installed my matlab2019a on local disc and now I successfully build my first project.
Also I move the "Generate JavaScript Using MATLAB Coder" folder to the default Add-Ons folder in local disc.
I think this will help me a lot on my current design.
Great tools! I shall try to learn more.

Geoff McVittie

@zhaozhou wu
Hello,
The Add-on which includes the Toolchain registration, project folder, and most of the API requires installation on a local drive; mapped network drives will generally fail and this is a known issue. To resolve this, I would recommend the following:
(1) Install the emscripten SDK onto a local drive, C:\ should be fine.
(2) In MATLAB, set the Add-Ons folder to a folder on the local drive. You can set the Add-Ons folder in MATLAB > Home > Environment > Preferences > MATLAB > Add-Ons. If MATLAB is installed on a locally drive, the Add-Ons folder will be on the drive and may not need to be changed.
(3) Create the project in a folder on a local drive.
Try these steps and let me know if the project can be created and built successfully.
Thank you,
Geoff McVittie

zhaozhou wu

Hi,there. I have tried to install this MATLAB coder.
I installed Emscripten SDK and I set the path by following the instructions given in Setup.mlx
the following are the results:
>> emsdkRootDir = webcoder.setup.getEmscriptenRootDirectory;
Emscripten SDK root folder provided:
D:\matlab2019\emsdk-master
>> webcoder.setup.verifyThirdPartyTools(emsdkRootDir);
EMSDK and external tools have been verified.
>> webcoder.setup.saveThirdPartyToolPaths(emsdkRootDir);
Third party toolpaths saved with WebCoder.
>> webcoder.setup.createAndRegisterToolchain;
Emscripten toolchain definition created.
Emscripten toolchain registered with MATLAB Coder.

As you can see, the Emscripten toolchain is created and registered successfuly.
However, when I run the "proj = webcoder.setup.project("faceTrace");" command in the command window, error occurs as following:

错误使用 coder.internal.setToolchainOptions
'Emscripten' is not a valid Toolchain name. Use one of the following: Automatically locate an installed toolchain
AUTOSAR Adaptive | CMake
AUTOSAR Adaptive Demonstrator
Microsoft Visual C++ 2017 v15.0 | nmake (64-bit Windows)
Microsoft Visual C++ 2015 v14.0 | nmake (64-bit Windows)
LCC-win64 v2.4.1 | gmake (64-bit Windows)
MinGW64 | gmake (64-bit Windows)
NVIDIA CUDA (w/Microsoft Visual C++ 2017) | nmake (64-bit Windows)
NVIDIA CUDA (w/Microsoft Visual C++ 2015) | nmake (64-bit Windows)
NVIDIA CUDA (w/Microsoft Visual C++ 2013) | nmake (64-bit Windows)
Mentor Graphics QuestaSim/Modelsim (64-bit Linux)
Cadence Incisive (64-bit Linux)
Cadence Xcelium (64-bit Linux)
Mentor Graphics QuestaSim/Modelsim (32-bit Windows)
Mentor Graphics QuestaSim/Modelsim (64-bit Windows)
Cadence Incisive (32-bit Linux)
Catkin
IBIS-AMI Microsoft Visual C++ 2017 v15.0 | nmake (64-bit Windows)
IBIS-AMI Microsoft Visual C++ 2015 v14.0 | nmake (64-bit Windows)
IBIS-AMI Microsoft Visual C++ 2013 v12.0 | nmake (64-bit Windows)
IBIS-AMI MinGW64 | gmake (64-bit Windows)

出错 webcoder.internal.project.createProject

出错 webcoder.setup.project (line 25)
proj = webcoder.internal.project.createProject(varargin{:});

I run the commands on matlab2019a, and I didn't come across the same promble discribed by michio. So I don't think this is a problem with version.

So do I get some suggestions? I think this may be some basic mistakes since I install my matlab on disc d instead of the default location on disc c.

Pavel Roslovets

michio

Geoff McVittie

This add-on has been updated to use the Emscripten SDK version 1.39. If you are using an earlier version of the Emscripten SDK, please update to the latest version in order to use this add-on.
Special thanks to @michio for discovering and reporting this issue.

michio

This sounds like a great tool! Thanks for your hard work.

I'm trying to set-up on my Windows machine using Setup.mlx but I get an error at webcoder.setup.createAndRegisterToolchain
Here's the output:

> emsdkRootDir = webcoder.setup.getEmscriptenRootDirectory;
Emscripten SDK root folder provided:
C:\Users\minoue\emsdk-master

> webcoder.setup.verifyThirdPartyTools(emsdkRootDir);
EMSDK and external tools have been verified.

> webcoder.setup.saveThirdPartyToolPaths(emsdkRootDir);
Third party toolpaths saved with WebCoder.

> webcoder.setup.createAndRegisterToolchain;
Dot indexing is not supported for variables of this type.

Error in webcoder.internal.coder.generateToolchainFile

Error in webcoder.setup.createAndRegisterToolchain (line 10)
webcoder.internal.coder.generateToolchainFile;

Geoff McVittie

@JongWeiChew
Hello,
An updated version of the Add-On is underdevelopment that will provide easy settings of the configuration flags, including the compiler optimizations. As a work around, you can use the MATLAB coder app. In the configurations, select the "Emscripten" toolchain. You can then set and add additional compilation flags and arguments; including the compiler optimizations. Please feel free to reach out to me though the email contact information provided in the installed Add-On if you need any further help.
Thank you,
Geoff McVittie

Geoff McVittie

@LeeHwayoung
Hello,
My initial guess is that the emscripten binaries need to be recompiled for your host computer architecture AMD64, see https://emscripten.org/docs/building_from_source/index.html. Please feel free to reach out to me though the email contact information provided in the installed Add-On for help with this process.
Thank you,
Geoff McVittie

Jing Wei Chew

Hi there, please advice - is it possible to make changes to the compilation flags used by Emscripten using this add-on?

Lee Hwayoung

c:\Users\lee\Desktop\emsdk>emsdk install latest
Warning: unknown machine architecture AMD64

Error: No tool or SDK found by name 'sdk-releases-fastcomp-7b4b328af02eafbc857b8ca1e3d9b12dddc56ef7-64bit'.

Could you please advise

Silas Henderson

Geoff McVittie

⚠ For Add-On version 1.0.3 and before, on Windows, install the archived emsdk-1.12.0-full-64bit from the archived releases at the bottom of the downloads page, https://emscripten.org/docs/getting_started/downloads.html, instead of the git installation.

Geoff McVittie

@Zachery Zhu
Hello,
Can you provide more detail on the problem you had with the installation?
On Windows 10, the steps should be almost exactly as follows:

1. In Windows 10, install Emscripten
C:\Users\<user>\Desktop> git clone https://github.com/emscripten-core/emsdk.git
C:\Users\<user>\Desktop> cd emsdk
C:\Users\<user>\Desktop> emsdk install latest
C:\Users\<user>\Desktop> emsdk activate latest

2. In MATLAB, install the "GenerateJavaScriptU​singMATLABCoder" using the Add-Ons Manager.

3. In MATLAB, open the Setup.mlx by running:
>> webcoder.setup.start()

4. Follow steps 3 to 6 in Setup.mlx

Please reach out if any of the above does not work. Please include any error message that is thrown.

Thank you,
Geoff

Zachery Zhu

I still couldn't install it correctly, haha. I'm using windows 10

Geoff McVittie

@Supannee Learkthanakhachon
Thank you for the feedback. An update, v1.0.3, has corrected the typos in the examples.
Can you provide more information on the specific issues experienced in installation process? I would like to better understand and, in the future, potentially correct these issues.

Please fix the typo in the example
>> server = webcoder.utilities.DeploymentServer("Port",8000)
it should be
>>server = webcoder.utilities.DevelopmentServer("Port",8000)
I spent a long time to figure this out. Plus...the entire installation process was a headache.

Larry Kyrala

Larry Kyrala

On Mac OSX, emscripten setup is done with bash scripts, so webcoder.setup.verifyThirdPartyTools failed with an error "BASH_SOURCE Undefined variable". You'll want to set MATLAB's shell to bash using something like: export MATLAB_SHELL=`which bash` before you verify.

MATLAB Release Compatibility
Created with R2019b
Compatible with R2019a to R2019b
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

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

Start Hunting!