Running a MATLAB script from Notepad++

2020-05-28 10:52发布

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.

3条回答
在下西门庆
2楼-- · 2020-05-28 11:21

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!

查看更多
啃猪蹄的小仙女
3楼-- · 2020-05-28 11:27

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.

查看更多
在下西门庆
4楼-- · 2020-05-28 11:28

This is a usable implementation upon Sam's idea. First, execute MATLAB in automation mode like this.

matlab.exe -automation

Next, compile and execute this following VB in NppExec plugin. (which is to use MATLAB automation API)

'open_matlab.vb
Imports System
Module open_matlab
    ' connect to a opened matlab session
    Sub Main()
        Dim h As Object
        Dim res As String
        Dim matcmd As String

        h = GetObject(, "Matlab.Application")
        Console.WriteLine("MATLAB & Notepad++")
        Console.WriteLine(" ")
        'mainLoop
        while True
            Console.Write(">> ")
            matcmd = Console.ReadLine()
            ' How you exit this app
            if matcmd.Equals("!!") then
                Exit while
            End if
            res=h.Execute(matcmd)
            Console.WriteLine(res)
        End while
    End Sub
End Module

Then you'll get a matlab-like terminal below your editor. You can then code above and execute below. type !! to exit the terminal. What it looks like

Tips: don't use ctrl+c to interrupt the MATLAB command, because it will kill the whole process instead.

查看更多
登录 后发表回答