加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

matlab调用dll文件

(2010-05-06 10:32:20)
标签:

matlab

dll

调用

it

分类: Matlab
addpath([matlabroot '\extern\examples\shrlib'])%matlabdll为例


loadlibrary shrlibsample shrlibsample.h alias lib%加载shrlibsample库,并重命名为lib,注意加载时常常需要shrlibsample的头文件  

 

libfunctionsview lib%显示lib库中的可用函数  

matlab调用dll文件
libfunctions lib -full%在命令窗口显示lib库函数  

 

 

Functions in library lib:

 

[double, doublePtr] addDoubleRef(double, doublePtr, double)

double addMixedTypes(int16, int32, double)

[double, c_structPtr] addStructByRef(c_structPtr)

double addStructFields(c_struct)

c_structPtrPtr allocateStruct(c_structPtrPtr)

voidPtr deallocateStruct(voidPtr)

lib.pointer exportedDoubleValue

lib.pointer getListOfStrings

doublePtr multDoubleArray(doublePtr, int32)

[lib.pointer, doublePtr] multDoubleRef(doublePtr)

int16Ptr multiplyShort(int16Ptr, int32)

doublePtr print2darray(doublePtr, int32)

printExportedDoubleValue

cstring readEnum(Enum1)

[cstring, cstring] stringToUpper(cstring)  

 

str = 'This was a Mixed Case string';

calllib('lib', 'stringToUpper', str)%调用lib库函数  

 

ans =

THIS WAS A MIXED CASE STRING  

 

如果lib库函数的参数为结构体,如下

double addStructFields(struct c_struct st)
{
    double t = st.p1 + st.p2 + st.p3;
    return t;
}

则要传递该结构体要用如下方法

sm.p1 = 476;   sm.p2 = -299;   sm.p3 = 1000;

sc = libstruct('c_struct', sm);%libstruct函数构造dll中的结构体,还可做初始化  

get(sc)%显示该结构体  

 

    p1: 476

    p2: -299

    p3: 1000  

 

calllib('lib', 'addStructFields', sc)%调用函数用calllib,参数依次为库名,库函数,库函数输入参数  

 

ans =

        1177  

 

clear sc %用完结构体要清除  

 

如果lib库函数的参数为指针,如下

void multiplyShort(short *x, int size)
{
    int i;
    for (i = 0; i < size; i++)
    *x++ *= i;
}

则要传递该指针要用如下方法

v = [4 6 8; 7 5 3];

pv = libpointer('int16Ptr', v);%libpointer函数来构造指针  

get(pv, 'Value')%显示指针的内容  

 

ans =

      4      6      8

      7      5      3  

 

calllib('lib', 'multiplyShort', pv, 6);%函数调用  

get(pv, 'Value')  

 

ans =

      0     12     32

      7     15     15  

clear

unloadlibrary lib%调用结束要卸载库  

 

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有