Vim Tips Wiki
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)
(Add breakpoint feature to the run script)
Tags: Visual edit apiedit
 
(5 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Matlab]]
 
[[Category:Windows]]
 
 
{{review}}
 
{{review}}
  +
{{TipImported
{{Tip
 
 
|id=1298
 
|id=1298
  +
|previous=1297
|title=A fast way to run Matlab script (for Windows users, Python required)
 
  +
|next=1299
|created=August 7, 2006 23:16
+
|created=2006
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Manu Hack
 
|author=Manu Hack
 
|version=n/a
 
|version=n/a
 
|rating=1/1
 
|rating=1/1
  +
|category1=Matlab
|text=
 
  +
|category2=Windows
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.)
 
 
}}
 
If you have a Matlab script file (not function file), then you could add the following simple files to <code>C:/Program Files/Vim/vimfiles/ftplugin/matlab</code> (depends on where you installed Vim).
   
run.py
+
run.py
  +
<pre>
for i in range(len(vim.current.buffer)):
 
print h.Execute(vim.current.buffer[i])
+
for i in range(len(vim.current.buffer)):
 
print h.Execute(vim.current.buffer[i])
run.vim
 
  +
</pre>
:py from win32com.client import Dispatch
 
:py import vim
 
:py h=Dispatch('matlab.application')
 
map &lt;buffer&gt; ,r :w&lt;CR&gt;:cd C:/Program Files/Vim/vimfiles/ftplugin/matlab&lt;CR&gt;:pyf run.py&lt;CR&gt;
 
   
 
run.vim
Then just type <tt>,r</tt> you can run and get the results in the current windows.
 
  +
<pre>
 
: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>
  +
</pre>
   
 
Then just type <code>,r</code> 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.
 
}}
 
   
  +
Make sure you have a running Python. I'm using WinXP, Vim 7.0, Matlab 6.5, and Python 2.4.
==See also==
 
*the resulting {{script|id=1634}}
 
   
  +
See the improved version of this tip in {{script|id=1634}}.
== Comments ==
 
Not working...
 
I created the files run.vim, run.py, and the map. However, when I enter a matlab script I get this:
 
   
 
==Comments==
Couldn't open buffer 3 in window 2, creating a new window
 
 
Not working. I created the files run.vim, run.py, and the map. However, when I enter a matlab script I get this:
Error message: NameError: name 'Dispatch' is not defined
 
   
  +
<pre>
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)
 
 
Couldn't open buffer 3 in window 2, creating a new window
 
Error message: NameError: name 'Dispatch' is not defined
  +
</pre>
   
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:
+
Just a thought on this one, perhaps it would be better to enclose run.vim in:
   
  +
<pre>
if has("win32")
+
if has("win32")
...
+
...
endif
+
endif
  +
</pre>
   
just to ensure it doesn't cause problems for those of use who use the same vim setup on Windows &amp; Linux. Also, it would be beneficial to change the mapping to use $VIM instead of C:\Program Files\vim
+
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.
 
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:
+
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.
+
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.
+
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
+
run.py
  +
<pre>
h.Execute("rehash toolboxcache")
+
h.Execute("rehash toolboxcache")
for i in range(len(vim.current.buffer)):
 
print h.Execute(vim.current.buffer[i])
+
for i in range(len(vim.current.buffer)):
 
print h.Execute(vim.current.buffer[i])
  +
</pre>
   
Then it should work well. At least at the moment I could start working without going back and forth between Matlab and Vim.
+
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
+
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.
   
  +
<pre>
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>
  +
map <buffer> ,b :w<CR>:py h.Execute("rehash path")<CR>:py print h.Execute('dbstop in '+vim.eval('expand("%:t:r")')+' at '+vim.eval('line(".")'))<CR>
 
endif
  +
</pre>
   
 
Still, you need Python with the win32com.
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 &lt;buffer&gt; ,r :w&lt;CR&gt;:py h.Execute("rehash path")&lt;CR&gt;:py print h.Execute(vim.eval('expand("%:t:r")'))&lt;CR&gt;
 
endif
 
   
  +
Tip: You might want to use 'matlab.desktop.application' for the complete matlab interface.
Still, you need python with the win32com.
 
   
manuhack--AT--gmail.com
 
, August 11, 2006 0:21
 
 
----
 
----
<!-- parsed by vimtips.py in 0.700768 seconds-->
 

Latest revision as of 20:45, 15 September 2015

Tip 1298 Printable Monobook Previous Next

created 2006 · complexity intermediate · author Manu Hack · version n/a


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.

See the improved version of this tip in script#1634.

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

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


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


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.


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>
  map <buffer> ,b :w<CR>:py h.Execute("rehash path")<CR>:py print h.Execute('dbstop in '+vim.eval('expand("%:t:r")')+' at '+vim.eval('line(".")'))<CR>
endif

Still, you need Python with the win32com.

Tip: You might want to use 'matlab.desktop.application' for the complete matlab interface.