How to emulate Matlab’s Run command (F5) from within Vim?

EDIT: See plugin made from it: https://github.com/elmanuelito/vim-matlab-behave

Sorry for replying to my own question. It's my bad, I was using xclip where I should have used xclip -selection c . I put below the different codes that work out for me: it reproduces the F5 and the evaluate cell behavior but you have to press Ctrl-V or use the mouse to paste in Matlab Command Window. If you are not on the command window, use matlab shortcut Ctrl-0 to jump to it. The script automatically goes from vim to matlab(unless you have another window with the name "matlab" in it). I added marks to go back to the former cursor position afterwards.

To run the whole script matlab: This is the "Run/F5" behavior (goes to the right directory and execute the whole script):

function! MatRun()
   normal mm " mark current cursor in mark m"
   let @+="cd('".expand("%:p:h")."\'); run('./".expand("%:f"). "')"
   call system('xclip -selection c ', @+)
   call system('xclip ', @+)
   normal `m "go back to cursor location"
   !wmctrl -a MATLAB 
endfunction
map ,m :call MatRun() <cr><cr>

To run, evaluate the cell only : This is the Ctrl+Enter behavior. In the same line than above: (edit: I now extended it so that it will work from the first upward %% OR the start of the file (\%^) till the first downward %% OR the end of the file (\^$) end edit)

function! MatRunCell()
    normal mm "remember cursor"
    :?%%\|\%^?;/%%\|\%$/w !xclip -selection c  "pipe the cell to xclip"
    normal `m "go back to cursor location"
    !wmctrl -a MATLAB  "go to matlab window"
endfunction
map ,k :call MatRunCell()  <cr><cr>

But I like better the following one, though more complicated (and could certainly be made vim-only). The following, ensures you are in the right directory to run the cell, and also goes back to Vim after evaluating the cell (if you have correctly configured the external editor in matlab. I use:gvim --servername MAT --remote-tab-silent).

 function! MatRunCellAdvanced()
     execute "!echo \"cd(\'".expand("%:p:h")."\')\">/tmp/buff"  
     normal mm
     :?%%\|\%^?;/%%\|\%$/w>> /tmp/buff
     execute "!echo \"edit ".expand("%:f")."\">>/tmp/buff"
     !cat /tmp/buff|xclip -selection c
     !cat /tmp/buff|xclip
     normal `m
     !wmctrl -a MATLAB 
 endfunction
map ,n :call MatRunCellAdvanced()  <cr><cr>

Run current line : Copy current line to clipboard and go to matlab window. (once again Control+V to paste it in matlab, if it's the short-cut you use in matlab)

 function! MatRunLine()
     " write current line and pipe to xclip "
     :.w !xclip -selection c
     !wmctrl -a MATLAB 
 endfunction
 map ,l :call MatRunLine()  <cr><cr>

Run selection : Emulates F9 behavior.

  function! MatRunSelect()
     normal mm
     !rm -f /tmp/buff
     :redir > /tmp/buff
     :echo @*
     :redir END
     execute "!echo \" \">>/tmp/buff"
     execute "!echo \"edit ".expand("%:p")."\">>/tmp/buff"
     !cat /tmp/buff|xclip -selection c
      normal `m
     !wmctrl -a MATLAB 
  endfunction

Bold Cell title Bold font in the cell title

 highlight MATCELL cterm=bold term=bold gui=bold
 match MATCELL /%%.*$/

Where to put those commands All of these commands in .vim/after/ftplugin/matlab.vim

edit Instead of using:

 run('./".expand("%:f")."')"

I now use

run('".expand("%:p")."')"

To deal with the case where vim 'pwd' is different from the script directory. I didn't find a way to extract just the script name, so I use the full path. Somebody can probably find a nicer solution for that.