Vim Tips Wiki
m (Added to Matlab + Windows cataegories ; minor reformating ; comments still need to be merged)
m (A fast way to run Matlab script (for Windows users, Python required) moved to Run Matlab script under Windows: Page moved by JohnBot to improve title)

Revision as of 10:32, 18 October 2007

Previous TipNext Tip

Tip: #1298 - Run Matlab script under Windows

Created: August 7, 2006 23:16 Complexity: intermediate Author: Manu Hack Version: n/a Karma: 1/1 Imported from: Tip#1298

If you have a Matlab script file (not function file), then you could add the following simple files to C:/Program Files/Vim/vimfiles/ftplugin/matlab. (Depends on where you installed Vim.)

run.py

for i in range(len(vim.current.buffer)): 
    print h.Execute(vim.current.buffer[i]) 

run.vim

:py from win32com.client import Dispatch 
:py import vim 
:py h=Dispatch('matlab.application') 
map <buffer> ,r :w<CR>:cd C:/Program Files/Vim/vimfiles/ftplugin/matlab<CR>:pyf run.py<CR> 

Then just type ,r you can run and get the results in the current windows.

Make sure you have a running python. I'm using WinXP, Vim 7.0, Matlab 6.5, and Python 2.4. You could modify the code if you find that the code doesn't work in your computer. Please feel free to comment on the code or improve it.

See also

Comments

Not working... I created the files run.vim, run.py, and the map. However, when I enter a matlab script I get this:

Couldn't open buffer 3 in window 2, creating a new window Error message: NameError: name 'Dispatch' is not defined

I am using WinXP, vim7, and Python2.4 (I just installed Python and included c:\python24 and c:\python24\dlls in the path, as I am no admin here. Apparently it worked, as I can call the Python prompt from any folder)

Anyway, it is a good idea. I hope I can make it work.

batman--AT--batcave.com , August 8, 2006 2:57


Just a thought on this one, perhaps it would be better to enclose run.vim in:

if has("win32") 
   ...
endif 

just to ensure it doesn't cause problems for those of use who use the same vim setup on Windows & Linux. Also, it would be beneficial to change the mapping to use $VIM instead of C:\Program Files\vim

Anonymous , August 8, 2006 2:59


Thanks for the comment.

If Dispatch doesn't exist, I'm guessing you may need to install Python Win32 Extensions if you don't have one in your Python. http://www.python.net/crew/mhammond/win32/Downloads.html

manuhack--AT--gmail.com , August 8, 2006 9:53


Another thing is that there should be a tab before the line in the for-loop. You may get an error for omitting that.

manuhack--AT--gmail.com , August 8, 2006 9:56


Some more updates for the code:

1. One may need to add the path where your functions are stored. This can be done by Matlab function addpath.

2. After using for a while, I find that if my script call other functions which have been modified, Matlab is not able to use the updated version of the functions. One could add a line in run.py as follows.

run.py

h.Execute("rehash toolboxcache") 
for i in range(len(vim.current.buffer)): 
    print h.Execute(vim.current.buffer[i]) 

Then it should work well. At least at the moment I could start working without going back and forth between Matlab and Vim.

manuhack--AT--gmail.com , August 8, 2006 13:09


Finally got some time to improve the run.vim

Now no need to use the run.py and just put the following run.vim in ftplugin/matlab.

if has("win32") 
  :py from win32com.client import Dispatch 
  :py import vim 
  :py h=Dispatch('matlab.application') 
  :py path=vim.eval('expand("%:p:h")') 
  :py h.Execute("temppath='"+ path+"'") 
  :py h.Execute("addpath(temppath)") 
  :py h.Execute("clear temppath") 
  map <buffer> ,r :w<CR>:py h.Execute("rehash path")<CR>:py print h.Execute(vim.eval('expand("%:t:r")'))<CR> 
endif 

Still, you need python with the win32com.

manuhack--AT--gmail.com , August 11, 2006 0:21