現在、Visual Studio で MATLAB を直接呼び出そうとしていますが、機能していないようです。
明確にするために、例として次のデモ ケースを取り上げます。つまり、MATLAB を使用して を計算し2+3
ます。結果、すなわちans = 5
が行に出力されるべきであることが予想されますprintf("%s\n", buf);
が、これは空であることが判明しました。ちなみに、MATLAB エンジンはengOpen()
正常に開かれています ( )。
#include <stdio.h>
#include <thread>
#include "engine.h"
Engine *matlab;
void thread_func()
{
// set printing buffer
char buf[1001];
engOutputBuffer(matlab, buf, 1000);
// call MATLAB
engEvalString(matlab, "2+3");
printf("%s\n", buf); // if ok, should print "ans = 5" in the command window
}
int main()
{
// Open MATLAB engine
matlab = engOpen(NULL);
if (matlab == NULL){
printf("Error to open MATLAB engine!!!\n");
exit(-1);
}
// use "Engine *matlab" in this thread
std::thread another_thread(thread_func);
// wait the thread to finish
another_thread.join();
// Close MATLAB engine
engClose(matlab);
return 0;
}
より詳しい情報:
- MATLAB バージョン: R2014a x64
- Visual Studio バージョン: 2013 プロフェッショナル
- プロジェクトのビルド プラットフォームも x64 に設定されています。
- コードの実行中に MATLAB コマンド ウィンドウが作成されますが、これは想定どおりです。