Matlab 版 (精华区)
发信人: billsun (MMMMMMMMM), 信区: Matlab
标 题: Re: 请问谁知道VC程序与Simulink的接口程序怎么写啊???
发信站: 哈工大紫丁香 (2002年05月29日14:24:06 星期三), 站内信件
这是一个vc跟matlab接口的做法,可以把matlab程序做成dll不知道你有没有用
How do I call a Compiler generated stand-alone DLL from my Visual Basic appl
ication in MATLAB 6.0 (R12)?
I would like to call a DLL generated by the MATLAB Compiler 2.1 and linked t
o the C/C++ Math Libraries from my Visual Basic application.
Solution:
PLEASE NOTE: If you are using MATLAB version 5.3.x please refer to the follo
wing URL:
http://www.mathworks.com/support/solutions/data/23697.shtml
For MATLAB 6.0 (R12):
This solution will provide you with a complete example that will take you fr
om an M-file (created in MATLAB 6.0) to a Visual Basic application that call
s a Compiler generated DLL. This example is intended to show you the steps t
o take you from start to finish and does not emphasize the functionality of
the DLL or Visual Basic application.
Please Note: There are many ways which this example can be changed to suit a
particular functionality. If you have any questions about this example, ple
ase refer to the documentation. We have provided examples in the manuals and
online references to assist you with modifying the code. For instance, many
questions that arise are about manipulating mxArrays (i.e. creating, extrac
ting data from, mulit-dimensional, etc.). To answer to these questions, you
can refer to Chapter 3 of the Application Program Interface Guide or refer t
o the MATLAB Helpdesk. To access this part of the Helpdesk:
1. Type "Helpdesk" at the MATLAB prompt. This will open the MATLAB Helpdesk
in your web browser.
2. Click on the "Application Program Interface Guide" link.
3. Click on the function of interest for a detailed reference.
Other common questions are related to the functionality of the routines in t
he Math Library. Please refer to the C/C++ Math Library User's Guide and als
o to the online reference in the Helpdesk. To access this part of the Helpde
sk, replace step 2 above with the following:
2. Click on the "MATLAB C Math Library 2.0 Ref." link.
We are willing to investigate any problem or question that you may have. How
ever, in order to resolve your issue, you must narrow down the problem to a
very few lines of code and create an example that does not require the Visua
l Basic or Visual C/C++ environment.
The following example was created using Microsoft Visual C/C++ 6.0 and Visua
l Basic 6.0. Instructions for other C compilers and/or versions of Visual Ba
sic are not available because it is not practical for us to offer complete t
echnical support on the details of using any specific one of the large numbe
r of IDE environments our customers use. If you need detailed assistance wit
h the particular settings needed to get your IDE environment to generate cod
e that successfully compiles and runs with our products, we suggest you cont
act the manufacturer of your IDE to get the necessary information or expert
technical assistance in using it.
NOTE: These instructions have been used successfully by many customers. Almo
st every problem reported to us has been the result of a typographical error
, skipped steps, or mistakes in compiler settings. If you encounter problems
with this example, please retrace or repeat these steps before contacting u
s.
In MATLAB,
1. Write a function named foo that contains the following code:
function y=foo(x)
y=x+1;
2. Use the MATLAB Compiler to compile foo.m into C code that can be included
in a C shared library.
mcc -W lib:foolib -L C -t foo.m
This will generate the following files.
foo.c
foo.h
foolib.c
foolib.h
foolib.mlib
foolib.exports
In Microsoft Visual C/C++ 6.0
1. Start up the Microsoft Visual C/C++ 6.0 IDE
2. Go to FILE and NEW. Click on Projects Tab. Select Win32 Dynamic-Link Libr
ary. In the Project Name field type: test. Create new workspace should be fi
lled in. In the Platforms field, Win32 should also be checked. Click OK.
3. Double click on test files. Highlight Sources Files and then right click.
Select Add files to folder. Find and choose the foo.c file (created above).
4. Double click on test files. Highlight Sources Files and then right click.
Select Add files to folder. Find and choose the foolib.c file (created abov
e).
5. Highlight test files. Choose from the menu, Project, Add to Project, New.
Under the Files tab choose C++ Source file. Name the file foowrap.c. Make s
ure Add to Project is checked. Make sure test appears in the listbox. Please
Note: Be sure to use the .c extension for the name of the file.
6. Enter the following code in the foowrap.c file:
#include "foolib.h"
#include "matlab.h"
double __stdcall foowrap(double x)
{
mxArray *x_ptr;
mxArray *y_ptr;
double *y;
double ret;
/* Create an mxArray to input into mlfFoo */
x_ptr = mlfScalar(x);
foolibInitialize();
y_ptr = mlfFoo(x_ptr);
/* The return value from mlfFoo is an mxArray so we must extract the data f
rom it */
y = mxGetPr(y_ptr);
ret = *y;
/* Return a double precision number to Visual Basic */
return(ret);
}
7. Highlight test files. Choose from the menu, Project, Add to Project, New.
Under the Files tab choose Text file. Name the file test.def. Make sure Add
to Project is checked. And make sure test appears in the listbox. Note: Be
sure to use the .def extension for the name of the file.
8. Enter the following code into the test.def file.
LIBRARY test.dll
EXPORTS
foowrap
9. Highlight test files. and then right click. Select Add Files to Project.
From $MATLAB\extern\lib\win32\microsoft\msvc60 directory select libmatlb.lib
, libmmfile.lib and libmx.lib.
Note: $MATLAB is the root directory.
10. Highlight test files and right click. Select Settings. Click on the C/C+
+ Tab. In the Category listbox select Code Generation. In the Use Runtime li
brary listbox select Multithreaded DLL. Change the Category listbox to Prepr
ocessor. Add to the Preprocessor definitions MSVC, MSWIND, IBMPC so:
WIN32,_DEBUG,_CONSOLE,_MBCS
changes to:
WIN32,_DEBUG,_CONSOLE,_MBCS,MSVC,MSWIND,IBMPC
Add to the Additional include directories field:
$MATLAB\extern\include;
$MATLAB\extern\include\cpp;
$MATLABR\extern\lib\win32\microsoft\msvc60;
Note: $MATLAB is the root directory.
Also add to the Additional include directories field the directory in which
foolib.h is located.
11. Go to Build and Rebuild All.
12. You should now have built test.dll.
In Visual Basic
1. Launch Visual Basic and choose a New Standard EXE Project.
2. Drag a Push Button into the Project.
3. Double click on the Push Button in Form 1. This will open a new window na
med Project1-Form1 (Code).
4. Choose General from the left listbox.
5. Enter the following code (you may need to enter the exact path to test.dl
l for the VB program to find this file):
Dim y As Double
Private Declare Function foowrap Lib "H:\Cases\vbdll\test\Debug\test.dll" (B
yVal x As Double) As Double
Private Sub Command1_Click()
y = foowrap(1.1)
MsgBox y
End Sub
6. Choose Run, Start
7. Push the button. A message box should now appear with the value 2.1.
【 在 bhh (月月) 的大作中提到: 】
: 我作毕业设计用Simulink仿真,其中要调用MATLAB中自带的arysin.c,
: arymimai.c,schdint.c这三个程序,这三个都是vc++程序.如果要在S-函数模块中调用这
: 些程序,
: 不能直接调用,要分别写一个vc程序与simulink的接口程序,使其变成.dll文件,才能调用
: 我不知道该怎么写,我非常非常着急,请大家帮帮忙,谢谢!!
: ~
: ~
--
※ 来源:·哈工大紫丁香 bbs.hit.edu.cn·[FROM: 202.102.144.58]
Powered by KBS BBS 2.0 (http://dev.kcn.cn)
页面执行时间:2.659毫秒