Skip to content

Instantly share code, notes, and snippets.

@jfhbrook
Created June 12, 2010 00:15
Show Gist options
  • Save jfhbrook/435224 to your computer and use it in GitHub Desktop.
Save jfhbrook/435224 to your computer and use it in GitHub Desktop.

matlab's cli interpreter is dumber than octave, in that it doesn't know what to do with the #! magic number, and it doesn't know to exit at the end of a file (I can actually see the point in that, kind of). So, this is my quick attempt to make something that would allow you to, well,

./test.m

It ALMOST works. ALMOST. No errors are thrown or anything! :v On the other hand, you don't actually see "hello world!!!!1!" either. Haven't tried anything like, say, printing to a file.

#!/usr/bin/python
import re
import sys
from os import system, path
filename=path.basename(sys.argv[-1])
infile=open(filename,'r')
outfile=open('methlabs_tmp_'+filename,'w')
#Drops the first line and any blank ones, dumps the result into
#a temporary file in the same directory
for pos, line in enumerate(infile):
if pos>0 and line!="\n":
outfile.write(line)
#Runs the file
system("matlab -nosplash -nodesktop -r 'methlabs_tmp_"+re.split(r'\.', filename)[0]+"; exit'")
#Deletes the tempfile
system('rm methlabs_tmp_'+path.basename(sys.argv[-1]))
#!/usr/bin/env methlabs
disp('hello world!!!!1!');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment