/*
「SimulinkでC言語のS-function使いたい」を試している時の自分用メモ
"サポートされているコンパイラまたは SDK が見つかりません。無償提供されている MinGW-w64 C/C++ コンパイラを
インストールできます。「Install MinGW-w64 Compiler」を参照してください。その他のオプションについては、
http://www.mathworks.com/support/compilers/R2015b/win64.html を参照してください。"
↓
Cのコンパイラがないと言われているらしい。
MATLABの中から呼んでも、インストールできなかったので、ブラウザで検索して、そこからダウンロードしたら
インストールできた(二日がかり)
↓
mex -setup
↓
"MEX は C 言語のコンパイルに 'MinGW64 Compiler (C)' を使用するよう設定されています。
警告: MATLAB C および Fortran API は、2^32-1 を超える要素のある MATLAB
変数をサポートするように変更されました。今後、
新しい API を利用できるようにコードを
更新する必要があります。詳細は、
http://www.mathworks.co.jp/jp/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html を参照してください。
別の言語を選択するには、次のいずれかを選択してください。
mex -setup C++
mex -setup FORTRAN"
↓
mex -setup C++
↓
mex timestwo.c
↓
"'MinGW64 Compiler (C)' でビルドしています。
MEX は正常に完了しました。"
↓
timestwo(4)
↓
エラー: timestwo
MEX level2 S-function "timestwo" must be called with at least 4 right hand arguments
↓
>> timestwo(1,1,0,0)
ans =
0
0
1
1
0
0
1
0
0
0
0
0
0
0
0
0
0
0
0
8480229
0
0
0
8194
0
0
0
0
0
0
0
2
0
0
0
0
0
0
0
0
↓
まったく何のことやら分からんので、今からソースコードの解析に入る
*/
/*
* File : timestwo.c
* Abstract:
* An example C-file S-function for multiplying an input by 2,
* y = 2*u
*
* See simulink/src/sfuntmpl.doc
*
* Copyright (c) 1990-1998 by The MathWorks, Inc. All Rights Reserved.
* $Revision: 1.3 $
*/
#define S_FUNCTION_NAME timestwo
#define S_FUNCTION_LEVEL 2
#include "simstruc.h"
/* Function: mdlInitializeSizes ===============================================
* Abstract:
* Setup sizes of the various vectors.
*/
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 0);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return; /* Parameter mismatch will be reported by Simulink */
}
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetInputPortDirectFeedThrough(S, 0, 1);
if (!ssSetNumOutputPorts(S,1)) return;
ssSetOutputPortWidth(S, 0, DYNAMICALLY_SIZED);
ssSetNumSampleTimes(S, 1);
/* Take care when specifying exception free code - see sfuntmpl.doc */
ssSetOptions(S, SS_OPTION_EXCEPTION_FREE_CODE);
}
/* Function: mdlInitializeSampleTimes =========================================
* Abstract:
* Specifiy that we inherit our sample time from the driving block.
*/
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
/* Function: mdlOutputs =======================================================
* Abstract:
* y = 2*u
*/
static void mdlOutputs(SimStruct *S, int_T tid)
{
int_T i;
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
real_T *y = ssGetOutputPortRealSignal(S,0);
int_T width = ssGetOutputPortWidth(S,0);
for (i=0; i