[英]How can I initialize a 2d Array with no rows or columns, and create a function that takes user input, then updates the 2d array?
[英]How can I create an s-function in Simulink with an input port that is a 2d array?
我正在嘗試使用s-function構建器在Simulink中創建一個s函數,它將接受一個二維數組作為輸入。 在輸入端口我指定尺寸:2d,rows:4,columns:4。當我嘗試使用f [x] [y]訪問輸入端口時,它給出一個錯誤:“錯誤C2109:下標需要數組或指針鍵入“,”用於輸入端口所在的行。
如何在Simulink中使用2d數組的輸入端口創建s函數?
相關代碼:
static void mdlInitializeSizes(SimStruct *S)
{
DECL_AND_INIT_DIMSINFO(inputDimsInfo);
DECL_AND_INIT_DIMSINFO(outputDimsInfo);
ssSetNumSFcnParams(S, NPARAMS);
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) {
return; /* Parameter mismatch will be reported by Simulink */
}
ssSetNumContStates(S, NUM_CONT_STATES);
ssSetNumDiscStates(S, NUM_DISC_STATES);
if (!ssSetNumInputPorts(S, NUM_INPUTS)) return;
/*Input Port 0 */
inputDimsInfo.width = INPUT_0_WIDTH;
ssSetInputPortDimensionInfo(S, 0, &inputDimsInfo);
ssSetInputPortMatrixDimensions( S ,0, INPUT_0_WIDTH, INPUT_DIMS_0_COL);
ssSetInputPortFrameData(S, 0, IN_0_FRAME_BASED);
ssSetInputPortDataType(S, 0, SS_DOUBLE);
ssSetInputPortComplexSignal(S, 0, INPUT_0_COMPLEX);
ssSetInputPortDirectFeedThrough(S, 0, INPUT_0_FEEDTHROUGH);
ssSetInputPortRequiredContiguous(S, 0, 1); /*direct input signal access*/
if (!ssSetNumOutputPorts(S, NUM_OUTPUTS)) return;
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 0);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);
/* Take care when specifying exception free code – see sfuntmpl_doc.c */
ssSetOptions(S, (SS_OPTION_EXCEPTION_FREE_CODE |
SS_OPTION_USE_TLC_WITH_ACCELERATOR |
SS_OPTION_WORKS_WITH_CODE_REUSE));
}
在mdlOuputs
我嘗試將f
(端口)視為普通數組。 例:
x=f[0][0];
這會引發錯誤。
編輯:好吧,有點想通了。
您可以根據輸入參數設置端口尺寸,然后可以使用f [x * xw + y]來處理這些值,其中x和y是x和y位置(從0開始),xw是列數。
還沒有找到更好的方法,但這有效。
我猜測S-Function構建器在mdlOutputs
生成的代碼如下mdlOutputs
:
real_T *y0 = (real_T *)ssGetOutputPortSignal(S, 0);
// OR
real_T *y0 = ssGetOutputPortRealSignal(S, 0);
對於任一行, y0
是指向1-D數組的指針,因此當您嘗試使用2個下標訪問它時,就像它是2-D數組一樣,編譯器會抱怨。
您可以通過將2-D索引更改為在編輯中發布的線性索引來修復它。 這非常合適,事實上,當您使用2個下標索引到2-D數組時,無論如何編譯器必須在幕后執行此操作。
另一種選擇是將ssGetInputPortSignal
(或ssGetInputPortRealSignal
)的返回值ssGetInputPortRealSignal
轉換為指向指針類型的指針 。
real_T **y0 = (real_T **)ssGetOutputPortSignal(S, 0);
y0[1][1] = 0;
正如您在編輯中提到的,使用線性索引實際上是在C MEX s函數中訪問矩陣的正確方法。 在sfun_matadd.c s-function示例中查看mdlOutputs: http : //www.ligo.caltech.edu/~rana/mat/Jenne/sfun_matadd.c 。 示例代碼中的注釋非常巧妙地解釋了它:
/*
* Note1: Matrix signals are stored in column major order.
* Note2: Access each matrix element by one index not two indices.
* For example, if the output signal is a [2x2] matrix signal,
* - -
* | y[0] y[2] |
* | y[1] y[3] |
* - -
* Output elements are stored as follows:
* y[0] --> row = 0, col = 0
* y[1] --> row = 1, col = 0
* y[2] --> row = 0, col = 1
* y[3] --> row = 1, col = 1
*/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.