Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running a MATLAB script from Notepad++

Is there a way of running a MATLAB script from Notepad++?

Obviously I have MATLAB installed on my computer. I know you can set a path for Notepad++to run when you hit F5, but when I set this path to my MATLAB.exe file, it simply opens another instance of MATLAB.

This is not what I want, I want the actual script in Notepad++ to be executed in the already open and running instance of MATLAB.

like image 355
Jonny Avatar asked Jun 02 '14 10:06

Jonny


2 Answers

I'm afraid I'm not on my home computer at the moment to test this out, so the following is just a suggestion for you to try.

If you take a look at the NppExec plugin for Notepad++, you'll see that with it you can specify a command to be run when you hit F6 (like an enhanced version of hitting F5 in the regular Notepad++). You can also give it variables such as the path to the current file, and the name of the current file.

MATLAB (on Windows at least - I assume you're on Windows) makes available an API over ActiveX/COM. If you search in the MATLAB documentation for details, it's under External Interfaces -> MATLAB COM Automation Server. By running (in MATLAB) the command enableservice('AutomationServer') you will set up your running instance of MATLAB to receive instructions over this API.

You should be able to write a small script (perhaps in VBScript or something similar) that will take as input arguments the path and filename of the current file in Notepad++, and will then connect to a running instance of MATLAB over the COM API and execute the file's contents.

Set this script to be executed in NppExec when you hit F6, and it should then run the current file in the open instance of MATLAB.

As I say, the above is just speculation as I can't test it out right now, but I think it should work. Good luck!

like image 200
Sam Roberts Avatar answered Oct 20 '22 02:10

Sam Roberts


Use NppExec add-on and press F6, copy paste the following and save the script:

NPP_SAVE
set local MATPATH=C:\Program Files\MATLAB\R2015a\bin\matlab.exe

cd "$(CURRENT_DIRECTORY)"

"$(MATPATH)" -nodisplay -nosplash -nodesktop -r "try, run('$(FILE_NAME)'), 
 catch me, fprintf('%s / %s\n',me.identifier,me.message), end"

then run (press F6; enter). Matlab Console and Plot windows still open and stay open. Error messages will be displayed in opening Matlab command window. Adding

, exit"

to the last command will make it quit and close again. If you want to run an automated application with crontabs or the like, check Matlab external interface reference for automation.

matlab.exe -automation ...

Also works in cmd terminal, but you have to fill in the paths yourself.

like image 29
Maik Holzhey Avatar answered Oct 20 '22 01:10

Maik Holzhey