#include "mex.h"
#define DWORD long
#define NUMBER_OF_STRUCTS (sizeof(friends)/sizeof(struct phonebook))
#define NUMBER_OF_FIELDS (sizeof(field_names)/sizeof(*field_names))
void mexFunction(int nlhs,
mxArray * plhs[] , int nrhs,const mxArray * pahs[])
{
typedef struct {
DWORD dt; //日期: 比如讀出數據為20031110,代表2003年11月10號
DWORD k_price;//開盤價*0.01):讀出的價格乘0.01為實際價格。下同。
DWORD h_price;//最高價*0.01
DWORD l_price;//最底價*0.01
DWORD s_price;//收盤價
float s_tolmoney;//(單位:億)
DWORD bur_tol;//成交量
DWORD unknown; ///沒有使用
}ST_K_DATE;
FILE * f = 0;
int nfieldidx= 0;
char * pkdata;
int lday = 8;
int dims[2] = {1, lday };
char msg[128] = "";
char path[512] = {0};
int i,r,c ,*val;
double *kp,*sp,*hp,*lp,*tm,*vol;
int lseekpos;
mxArray *field_value;
const char *field_names[] = {"日期", "開盤價","最高價","最低價","收盤價","成交金額","成交量"};
ST_K_DATE * pd = 0;
r = mxGetM(pahs[0]);
c = mxGetN(pahs[0]);
sprintf(msg,"r:%d , c:%d rn",r,c);
mexPrintf(msg);
mxGetString(pahs[0],msg,128);
sprintf(path,"D:\jcb_hxzq\vipdoc\sh\lday\sh%s.day",msg);
mexPrintf(path);
lday = (int)*mxGetPr(pahs[1]);
dims[1] = lday;
pkdata = malloc (sizeof(ST_K_DATE)*lday);
plhs[0] = mxCreateStructArray(2, dims, NUMBER_OF_FIELDS, field_names);
plhs[1] = mxCreateDoubleMatrix(1,lday,mxREAL);
plhs[2] = mxCreateDoubleMatrix(1,lday,mxREAL);
plhs[3] = mxCreateDoubleMatrix(1,lday,mxREAL);
plhs[4] = mxCreateDoubleMatrix(1,lday,mxREAL);
plhs[5] = mxCreateDoubleMatrix(1,lday,mxREAL);
plhs[6] = mxCreateDoubleMatrix(1,lday,mxREAL);
f = fopen(path,"r+b");
lseekpos = sizeof(ST_K_DATE)*lday;
fseek(f,-lseekpos,SEEK_END);
fread(pkdata,sizeof(ST_K_DATE),lday,f);
pd = (ST_K_DATE*)pkdata;
for (i=0;i<lday;i++){
nfieldidx = 0;
msg[0] = 0;
sprintf(msg,"%d",pd[i].dt);
mxSetField(plhs[0],i,field_names[nfieldidx++],mxCreateString(msg));
field_value = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[nfieldidx])[i] = *mxGetPr(field_value) = pd[i].k_price*0.01f;
mxSetField(plhs[0],i,field_names[nfieldidx++],field_value);
field_value = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[nfieldidx])[i] = *mxGetPr(field_value) = pd[i].h_price*0.01f;
mxSetField(plhs[0],i,field_names[nfieldidx++],field_value);
field_value = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[nfieldidx])[i] = *mxGetPr(field_value) = pd[i].l_price*0.01f;
mxSetField(plhs[0],i,field_names[nfieldidx++],field_value);
field_value = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[nfieldidx])[i] = *mxGetPr(field_value) = pd[i].s_price*0.01f;
mxSetField(plhs[0],i,field_names[nfieldidx++],field_value);
field_value = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[nfieldidx])[i] = *mxGetPr(field_value) = pd[i].s_tolmoney;
mxSetField(plhs[0],i,field_names[nfieldidx++],field_value);
field_value = mxCreateDoubleMatrix(1,1,mxREAL);
mxGetPr(plhs[nfieldidx])[i] = *mxGetPr(field_value) = pd[i].bur_tol;
mxSetField(plhs[0],i,field_names[nfieldidx++],field_value);
}
fclose(f);
}
-------------
在matlab中輸入 mex welcom.c ; [s,kp,hp,lp,sp,tm,vol] = welcom('600477',10);candle(kp',lp',sp',kp', 'red');
生成圖形如下:
爽。。後面可以開始使用matlab強大的功能分析數據了。