Showing posts with label Automatic. Show all posts
Showing posts with label Automatic. Show all posts

Friday, March 20, 2015

[vim] auto complete matlab flow control [for, if, while] with indentation in vim

Okay, it turns out just combining some keys is good enough :(

:inoremap <c-n><c-n> <esc>yyp^d$a
:inoremap <c-c><c-c> <esc>yyp^d$aend<esc>kyyp^d$a
 
hit ctrl-c twice (holding ctrl) in the edit mode , use ctrl-n twice to go next line

------------------------------------------------------------------- no need something huge --------------------------
"=============================================================================
" matlabFlowComplete.vim v0.0
" Author: Xianmin Hu <xianmin.hu@gmail.com>
" http://scriptdemo.blogspot.com
" Last Change: 19-Mar-2015.
"=============================================================================
"
"key cc : to complete for, if and while flow
"key nn : go to next line with proper indentation

:function! CompleteMatlabFlow()
" complete for, while and if flow control with proper indentation
: let isValidForWhileIf=0
: let tmpline=substitute(getline(".")," \\+"," ","g")
: let tmpline=substitute(tmpline," \\+$","","g")
: call setline(".",tmpline)
: let tmpline=substitute(tmpline,"^ \\+","","g")
: let clinesp=split(tmpline," ")
: unlet tmpline
: execute "normal! o"
: execute "normal! kk^"
: if len(clinesp) > 1
:    if clinesp[0] == "for" && len(clinesp)>2
:       let isValidForWhileIf=1
:       if len(clinesp)==3
:          let newcline="for " . clinesp[1] . " = " . clinesp[2] . ":1:" . clinesp[2]
:       elseif len(clinesp)==4
:         if clinesp[2]<clinesp[3]
:            let newcline=clinesp[0] . " " . clinesp[1] . " = " . clinesp[2] . ":" . clinesp[3]
:         else
:           let newcline=clinesp[0] . " " . clinesp[1] . " = " . clinesp[2] . ":-1:" . clinesp[3]
:        endif
:      elseif len(clinesp)>=5
:       let newcline=clinesp[0] . " " . clinesp[1] . " = " . clinesp[2] . ":" . clinesp[3] . ":" . clinesp[4]
:      endif
:      let cpos = col(".")
:      let preline=split(getline(".")," ")
:      if len(preline) > 0
:         if preline[0] == "for"
:            let cpos += 4
:         elseif preline[0] == "while"
:            let cpos += 6
:         elseif preline[0] == "if"
:            let cpos += 3
:         endif
:      endif
:      while cpos > 1
:               let newcline = " " . newcline
:               let cpos -= 1
:       endwhile
:       execute "normal! j"
:       call setline(".",newcline)
:       execute "normal! yyp"
:       execute "normal! ^d$"
:       call setline(".",getline(".") . "end")
:       execute "normal! jdd"
:       execute "normal! kyykpd$i "
:       unlet newcline
:       unlet cpos
:    endif
"
:    if (clinesp[0] == "if" || clinesp[0] == "while") && len(clinesp)>1
:       let isValidForWhileIf=1
:       let cpos = col(".")
:       let preline=split(getline(".")," ")
:       if len(preline) > 0
:          if preline[0] == "for"
:             let cpos += 4
:          elseif preline[0] == "while"
:             let cpos += 6
:          elseif preline[0] == "if"
:             let cpos += 3
:          endif
:       endif
:       let padStr=""
:       while cpos > 1
:           let padStr = " " . padStr
:           let cpos -= 1
:      endwhile
:      execute "normal! j^0d^"
:      call setline(".", padStr . getline("."))
:      execute "normal! yyp^d$"
:      call setline(".",getline(".") . "end")
:      execute "normal! jdd"
:      execute "normal! kyykpd$i "
:      unlet padStr
:      unlet cpos
:   endif
:   unlet clinesp
: endif
: if isValidForWhileIf < 1
:    execute "normal! u"
:    execute "normal! $"
: endif
: unlet isValidForWhileIf
:endfunction

:function! GoMatlabNext()
" go next line and use same indent from previous line
: execute "normal! yyp"
: execute "normal! ^d$a"
:endfunction

:nmap cc :call CompleteMatlabFlow()<CR>a
:nmap nn :call GoMatlabNext()<CR>a
Example:
1) type "for a 1 4", then run cc in normal mode, ==>
for a = 1:4

end
2) type "for a 4 1", then run cc in normal mode, ==>
for a = 4:-1:4

end
3) type "for a 2 4 12", then run cc in normal mode, ==>
for a = 2:4:12

end
4) type "while a =4", then run cc in normal mode, ==>
while a=4

end

5) auto-indent
if you have"
%==============
if isLoop==1
for n loopStart loopStep loopEnd (run cc in normal mode)
end

it will result in:
%==============
if isLoop==1
   for n = loopStart:loopStep:loopEnd
         
   end
end

Wednesday, November 17, 2010

Run matlab m-file in a shell script

It is common to run Matlab without splash and desktop using the following command,
        matlab -nosplash -nodesktop
Sometime the "-nojvm" option also help to reduce the memory requires if the java-virtual-mathine features are not necessary in your script.

But, what should we do if we want to run a Matlab m-file in a shell script? The only thing we need is the option of "-r",

        matlab -nosplash -nodesktop -r YourMFile

However, sometimes it will be much more convenient if we can run the function-type m-file with arguments. Here it comes today's tricky stuff:       
        matlab -nodesktop -nosplash -r YourMFile\(Number,\'character\'\)
Matlab Start Help 

Here is an example,
runMatlabScript.sh

#!/bin/bash
myNum=30
myT="Demo\ of\ Peaks\(${myNum}\)"
myPNG="fig_peaks_${myNum}"
eval "/media/SW_Preload/matlab/bin/matlab -nodesktop -nosplash -r showPeaks\(${myNum},\'${myT}\',\'${myPNG}\'\)"
showPeaks.m:
function showPeaks(myN,myTitle,myPNG)
% show the the peaks
% usage:
%           showPeaks(N,Title,FileName)
%           http://scriptdemo.blogspot.ca

if nargin~=3
   disp('Usage: ')
   disp(' showPeaks(N,Title,FileName) ')
   return
end

hf=figure('visible','off');
set(hf,'color','w','render','zbuffer');
hp=pcolor(peaks(myN));
set(hp,'linestyle','none');
title(myTitle,'fontweight','bold','fontsize',16);
set(gca,'linewidth',2)
eval(['print -dpng -r300 ',myPNG,'.png'])
close;
exit

Friday, December 19, 2008

Login into to ssh server automatically with expect

#!/bin/expect -f
# Connect to the server, please change the username and remote server address
#   http://scriptdemo.blogspot.com

spawn ssh USERNAME@serveraddress
#send the password to the server, please change it for "send" line
expect "password:"
send "password\n"
interact

Saturday, August 23, 2008

Download files automatically from ftp site

Download.bat
@echo off
@echo EmailAddress
echo Open FTPAdress>ftp.cfg
echo User USERNAME>>ftp.cfg
echo PASSWORD >>ftp.cfg
echo binary >>ftp.cfg
echo cd /datadir/ >>ftp.cfg
echo cd F08 >>ftp.cfg
echo cd MONTHLY >>ftp.cfg
set b=08
for /L %%a in (1989,1,1991) DO call subget %%a %b%
echo cd ..>>ftp.cfg
echo cd..>ftp.cfg
echo cd F11 >>ftp.cfg
echo cd MONTHLY >>ftp.cfg
set b=11
for /L %%a in (1991,1,1995) DO call subget %%a %b%
echo cd ..>>ftp.cfg
echo cd..>>ftp.cfg
echo cd F13 >>ftp.cfg
echo cd MONTHLY >>ftp.cfg
set b=13
for /L %%a in (1995,1,2006) DO call subget %%a %b%
echo cd ..>>ftp.cfg
echo cd..>>ftp.cfg
echo bye >>ftp.cfg
cls
ftp -n -s: "ftp.cfg" > Download_log.txt

subget.bat:
echo cd %1 >>ftp.cfg
echo get f%2-%1-tnn-00.tar>>ftp.cfg
echo get f%2-%1-tnn-05.tar>>ftp.cfg
echo get f%2-%1-tnn-10.tar>>ftp.cfg
echo get f%2-%1-tnn-15.tar>>ftp.cfg
echo cd ..>>ftp.cfg

After Run the first batch file, we got ftp.cfg as follows:
Open **.**.**.**
User Name
**@**
binary
cd *****
cd F08
cd MONTHLY
cd 1989
get f08-1989-tnn-00.tar
get f08-1989-tnn-05.tar
get f08-1989-tnn-10.tar
get f08-1989-tnn-15.tar
cd ..
cd 1990
get f08-1990-tnn-00.tar
get f08-1990-tnn-05.tar
get f08-1990-tnn-10.tar
get f08-1990-tnn-15.tar
cd ..
cd 1991
get f08-1991-tnn-00.tar
get f08-1991-tnn-05.tar
get f08-1991-tnn-10.tar
get f08-1991-tnn-15.tar
cd ..
cd ..
cd ..
cd F11
cd MONTHLY
cd 1991
get f11-1991-tnn-00.tar
get f11-1991-tnn-05.tar
get f11-1991-tnn-10.tar
get f11-1991-tnn-15.tar
cd ..
cd 1992
get f11-1992-tnn-00.tar
get f11-1992-tnn-05.tar
get f11-1992-tnn-10.tar
get f11-1992-tnn-15.tar
cd ..
cd 1993
get f11-1993-tnn-00.tar
get f11-1993-tnn-05.tar
get f11-1993-tnn-10.tar
get f11-1993-tnn-15.tar
cd ..
cd 1994
get f11-1994-tnn-00.tar
get f11-1994-tnn-05.tar
get f11-1994-tnn-10.tar
get f11-1994-tnn-15.tar
cd ..
cd 1995
get f11-1995-tnn-00.tar
get f11-1995-tnn-05.tar
get f11-1995-tnn-10.tar
get f11-1995-tnn-15.tar
cd ..
cd ..
cd ..
cd F13
cd MONTHLY
cd 1995
get f13-1995-tnn-00.tar
get f13-1995-tnn-05.tar
get f13-1995-tnn-10.tar
get f13-1995-tnn-15.tar
cd ..
cd 1996
get f13-1996-tnn-00.tar
get f13-1996-tnn-05.tar
get f13-1996-tnn-10.tar
get f13-1996-tnn-15.tar
cd ..
cd 1997
get f13-1997-tnn-00.tar
get f13-1997-tnn-05.tar
get f13-1997-tnn-10.tar
get f13-1997-tnn-15.tar
cd ..
cd 1998
get f13-1998-tnn-00.tar
get f13-1998-tnn-05.tar
get f13-1998-tnn-10.tar
get f13-1998-tnn-15.tar
cd ..
cd 1999
get f13-1999-tnn-00.tar
get f13-1999-tnn-05.tar
get f13-1999-tnn-10.tar
get f13-1999-tnn-15.tar
cd ..
cd 2000
get f13-2000-tnn-00.tar
get f13-2000-tnn-05.tar
get f13-2000-tnn-10.tar
get f13-2000-tnn-15.tar
cd ..
cd 2001
get f13-2001-tnn-00.tar
get f13-2001-tnn-05.tar
get f13-2001-tnn-10.tar
get f13-2001-tnn-15.tar
cd ..
cd 2002
get f13-2002-tnn-00.tar
get f13-2002-tnn-05.tar
get f13-2002-tnn-10.tar
get f13-2002-tnn-15.tar
cd ..
cd 2003
get f13-2003-tnn-00.tar
get f13-2003-tnn-05.tar
get f13-2003-tnn-10.tar
get f13-2003-tnn-15.tar
cd ..
cd 2004
get f13-2004-tnn-00.tar
get f13-2004-tnn-05.tar
get f13-2004-tnn-10.tar
get f13-2004-tnn-15.tar
cd ..
cd 2005
get f13-2005-tnn-00.tar
get f13-2005-tnn-05.tar
get f13-2005-tnn-10.tar
get f13-2005-tnn-15.tar
cd ..
cd 2006
get f13-2006-tnn-00.tar
get f13-2006-tnn-05.tar
get f13-2006-tnn-10.tar
get f13-2006-tnn-15.tar
cd ..
cd ..
cd ..
bye

ShowCalendar