@@ -398,7 +398,7 @@ async function getMatlabLaunchCommand (outFile: string): Promise<{ command: stri
398398 '-memmgr' , 'release' , // Memory manager
399399 '-noAppIcon' , // Hide MATLAB application icon in taskbar/dock, if applicable
400400 '-nosplash' , // Hide splash screen
401- '-r' , `addpath(fullfile(' ${ __dirname } ', '..', 'matlab')); initmatlabls(' ${ outFile } ')` , // Startup command
401+ '-r' , getMatlabStartupCommand ( outFile ) , // Startup command
402402 '-useStartupFolderPref' , // Startup folder flag
403403 '-nodesktop' // Hide the MATLAB desktop
404404 ]
@@ -418,3 +418,23 @@ async function getMatlabLaunchCommand (outFile: string): Promise<{ command: stri
418418 args
419419 }
420420}
421+
422+ /**
423+ * Gets the MATLAB command which the MATLAB application should run at startup.
424+ *
425+ * Note: This will sanitize the file paths so that they can be safely used within
426+ * character vectors in MATLAB. This is done by replacing all single-quote characters
427+ * with double single-quotes.
428+ *
429+ * @param outFile The file in which MATLAB should output connection details
430+ * @returns The MATLAB startup command
431+ */
432+ function getMatlabStartupCommand ( outFile : string ) : string {
433+ // Sanitize file paths for MATLAB:
434+ // Replace single-quotes in the file path with double single-quotes
435+ // to preserve the quote when used within a MATLAB character vector.
436+ const extensionInstallationDir = __dirname . replace ( / ' / g, "''" )
437+ const outFilePath = outFile . replace ( / ' / g, "''" )
438+
439+ return `addpath(fullfile('${ extensionInstallationDir } ', '..', 'matlab')); initmatlabls('${ outFilePath } ')`
440+ }
0 commit comments