This gist demonstrates how we can attach the D3 library, provided as a remote .js
file, to our uifigure and use it to create some interesting visualizations. The JS code in this example is taken almost verbatim from the Particles example, found inside the D3 example gallery.
- Save the three code files (
.m
,.js
,.css
) in the same folder, and execute the MATLAB script. - If there is some error such as
it only means that the figure didn't finish loading. Usually running the script again helps; if it doesn't, increase theError using matlab.internal.webwindow/executeJS (line 730) Error executing JavaScript command: JavaScript error: Uncaught ReferenceError: d3 is not defined at line 1 column 0 in
pause
durations; if that doesn't help either, just execute the code line by line.
- I've been experimenting with various examples until I found one that works with minimal adaptations for MATLAB.
- Some examples require a
canvas
element. As it happens, spawning auiaxes
creates acanvas
. Sincecanvas
implementations differ slightly between browsers, thecontextType
supported in uifigures (I only tested this on R2018a) iswebgl
(v1). - The delay which we change using
setCoalescerMaxDelay()
, is set by default to a duration of 10 days in milliseconds. It appears to be needed to synchronizedrawnow
events between MATLAB and JS. As noted in the code, I have no idea what sinister side-effects there are to changing this value, as it disables some workaround put in place by TMW.