SewiGの日記
2007-06-11 [月]
■ [MATLAB] 処理が終わったら通知
MATLABは便利なんだけど、計算に時間がかかる場合は計算終了時に通知してくれるとうれしいわけです。
MATLABにはCとかJavaとかと連携できる仕組みがあるのだけどおのおのの言語で書いたコードをMATLAB用に書き直すのが面倒なので、引数をそのままシェルで実行するだけのコードを書けば、何でもできますね。
// exec.c
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
char *input;
int length, status;
/* 引数が1つだけあるかどうか? */
if (nrhs != 1) {
mexErrMsgTxt("One input required.");
} else if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments.");
}
/* 入力が文字列かどうか? */
if ( mxIsChar(prhs[0]) != 1)
mexErrMsgTxt("Input must be a string.");
/* 入力が列ベクトルかどうか? */
if (mxGetM(prhs[0]) != 1)
mexErrMsgTxt("Input must be a row vector.");
/* 文字列の長さ */
length = (mxGetM(prhs[0]) * mxGetN(prhs[0])) + 1;
/* メモリ確保 */
input = mxCalloc(length, sizeof(char));
/* 文字列の取得 */
status = mxGetString(prhs[0], input, length);
if (status != 0) {
mexWarnMsgTxt("Not enough space. String is truncated.");
}
/* 以下に処理を書く */
system(input);
}
そして、MATLABのコンソールで
>> mex exec.c
あとは、MATLABのコードで好きな場所に通知したいコード書くだけ。
>> exec 'echo "Finished your MATLAB task" | mail hoge@example.com'
今流行りのTwitterに通知するならcurlなら1行で更新できるので便利。
>> exec 'curl -u username:password -d status="Finished your MATLAB task" http://twitter.com/statuses/update.xml'
本日のツッコミ(全3件) [ツッコミを入れる]
● SewiG [補足ですが、単にシェルにコマンドを渡すだけなら「!」で可能です。 MATLABの苦手な処理を外部に投げて、処理された..]
● xuliqan [If I were a Teengae Mutant Ninja Turtle, now I'd say "Kowa..]
● angel1 [Regards. I enjoy it. This is really interesting, You are..]