#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强大的功能分析数据了。