Last active
June 24, 2020 06:17
-
-
Save Ishotihadus/a0c95f7679e569b363b58ec210029f79 to your computer and use it in GitHub Desktop.
MATLAB の .m ファイルをスクリプトっぽく実行するシェルスクリプト。追加の引数は文字列として関数の引数に渡せる。Usage: `matlabs [-path /path/to/matlab] [-matlab-options] /path/to/script.m [arg1] [arg2] [...]`。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
local -A opts | |
local -a matlab_opts args | |
zparseopts -D -M -A opts -- h help=h path: -path:=path \ | |
c: noFigureWindows softwareopengl nosoftwareopengl debug singleCompThread \ | |
nojvm jdb: sd: useStartupFolderPref logfile: nouserjavapath | |
usage() { | |
echo 'MATLAB の関数をスクリプトライクに実行するコマンドです。' 1>&2 | |
echo "usage: $0 [-path MATLAB path] [-matlab-options] script.m [arg1] [arg2] [...]" 1>&2 | |
echo "usage: $0 [-path MATLAB path] [-matlab-options] -- - < (stdin)" 1>&2 | |
} | |
if [[ -n "${opts[(i)-h]}" ]]; then | |
usage | |
exit | |
fi | |
BIN=matlab | |
for key in ${(k)opts}; do | |
if [ $key = '-path' ]; then | |
BIN="${opts[-path]}" | |
continue | |
fi | |
matlab_opts+="$key" | |
if [ -n "${opts[$key]}" ]; then | |
matlab_opts+="${opts[$key]}"; fi | |
done | |
if [ $# -lt 1 ]; then | |
echo '関数ファイルを指定してください' 1>&2 | |
echo 1>&2 | |
usage | |
exit 1 | |
fi | |
if [ $1 = '-' ]; then | |
script="$(cat -)" | |
if [ $# -ge 2 ]; then | |
echo 'additional arguments are ignored when stdin is used' 1>&2 | |
fi | |
set -x | |
$BIN -nosplash -nodesktop $matlab_opts -batch "${script}" | |
else | |
if [ $# -ge 2 ]; then | |
for i in {2..$#}; do | |
args+="'${@[$i]/\'/\'\'}'" | |
done | |
fi | |
fullpath=$(readlink -f $1) | |
funcdir=${fullpath%/*} | |
funcname=${${fullpath%.m}##*/} | |
set -x | |
$BIN -nosplash -nodesktop $matlab_opts -batch "addpath('${funcdir}'); ${funcname}(${(j:, :)args});" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment