[問題] c structure轉matlab疑問
各位大大好!
我目前在寫C轉matlab的程式
也就是要用mex去編譯.c檔
但是現在碰到一個關於structure的傳遞問題
目前我的構想是透過matlab初始一個structure
然後再將這個structure輸出(return)
等等把C code貼在最下方
先說我目前碰到的問題
現在我已經可以把創起來的struct初始成功
輸出裡面的資料要確認也都OK
但是就在她要把這個struct傳出來(return)的時候
出錯了!!!!
而且這個錯誤是會crash掉matlab的
已經搞好久了
還是無解~
原本以為是沒有把初始好的struct回存到plhs[0]
所以使用mxSetData把pointer回存回去
但好像還是不行
所以希望版上有經驗的大大可以幫幫我
幫我找出問題點在哪邊
可以順利輸出~拜託拜託!
感恩
---------- 以下是程式碼 ----------
#include "mex.h"
#define Uint8 unsigned char
#define Int8 char
#define Uint16 unsigned short
#define Int16 short
#define Uint32 unsigned int
#define Int32 int
#define NUMBER_OF_STRUCTS (sizeof(*img)/sizeof(ImgInfo))
#define NUMBER_OF_FIELDS (sizeof(field_names)/sizeof(*field_names))
typedef struct
{
Uint8 depth;
Uint16 imgWidth;
Uint16 imgHeight;
Uint16 *imgData;
}ImgInfo;
void Init(ImgInfo *img)
{
img->depth = 2;
img->imgWidth = 256;
img->imgHeight = 256;
img->imgData = (Uint16*)malloc(sizeof(ImgInfo));
memset(img->imgData, 0, sizeof(Uint16)*img->imgWidth*img->imgHeight);
}
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
const char *field_names[] = {"depth", "imgWidth", "imgHeight", "imgData"};
ImgInfo *img;
mwSize dims[2] = {1, NUMBER_OF_STRUCTS};
plhs[0] = mxCreateStructArray(2, dims, NUMBER_OF_FIELDS, field_names);
img = mxGetPr(plhs[0]);
Init(img);
printf("%d\n", img->depth);
printf("%d\n", img->imgWidth);
printf("%d\n", img->imgHeight);
mxSetData(plhs[0], img);
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.25.174.108
※ 編輯: vulmoon 來自: 114.25.174.108 (12/19 22:33)
推
12/19 23:00, , 1F
12/19 23:00, 1F
→
12/19 23:05, , 2F
12/19 23:05, 2F
推
12/20 00:58, , 3F
12/20 00:58, 3F
→
12/20 07:24, , 4F
12/20 07:24, 4F
→
12/20 07:25, , 5F
12/20 07:25, 5F
推
12/20 14:55, , 6F
12/20 14:55, 6F
→
12/20 14:55, , 7F
12/20 14:55, 7F
→
12/20 14:56, , 8F
12/20 14:56, 8F
→
12/20 19:00, , 9F
12/20 19:00, 9F
→
12/20 19:01, , 10F
12/20 19:01, 10F
→
12/20 19:05, , 11F
12/20 19:05, 11F
推
12/21 02:31, , 12F
12/21 02:31, 12F
→
12/21 02:32, , 13F
12/21 02:32, 13F
→
12/21 19:04, , 14F
12/21 19:04, 14F
→
12/21 19:04, , 15F
12/21 19:04, 15F
推
12/22 03:14, , 16F
12/22 03:14, 16F
→
12/22 03:14, , 17F
12/22 03:14, 17F
→
12/22 07:34, , 18F
12/22 07:34, 18F
→
12/22 07:35, , 19F
12/22 07:35, 19F
→
12/22 07:35, , 20F
12/22 07:35, 20F
推
12/22 16:57, , 21F
12/22 16:57, 21F
→
12/22 16:57, , 22F
12/22 16:57, 22F
→
12/23 12:17, , 23F
12/23 12:17, 23F
→
12/23 12:17, , 24F
12/23 12:17, 24F