필터 지우기
필터 지우기

How to convert a function handle to a integer

조회 수: 4 (최근 30일)
Clownfish
Clownfish 2017년 3월 1일
답변: Walter Roberson 2017년 3월 3일
Hi, I need to call a Delphi function, that requires a function handle converted to an Integer. Here is the original Delphi dode:
Instantiate(cParameter, Integer(@MyCallback)) , where cParameter is some double.
I would call the Instantiate function via a library call
calllib('myDelphiLib','Instatiate',cParameter, ? )
What to fill in for the question mark? How do I pass a function handle that needs to converted to an Integer in Matlab?
Thanks for your help

답변 (2개)

Walter Roberson
Walter Roberson 2017년 3월 3일
When I look at your code, I get the impression that MyCallback will be a Delphi-Coded routine, rather than a MATLAB function handle. If so, then is there a call you can make into Delphi to get it to return @MyCallback ?
If there is, then you can handle the situation in MATLAB using lib.pointer, perhaps created with the convenience function libpointer() .
If there it is a Delphi routine you need the pointer to and there is no call you can make from outside Delphi to get the pointer, then you could have difficulty. Depending how Delphi manages external names, there is the possibility that the name is private to the Delphi file it is declared in (I am not familiar with Delphi linkage conventions.)
If it is a MATLAB function handle whose address you need to pass in to Delphi, then that situation could make sense if you were setting up a situation where Delphi become responsible for calling back to MATLAB. I would, though, want to know more about the callback mechanism; for example if it were by using the MATLAB Engine API then this is not the way I would proceed.
If you are trying to pass in a MATLAB function handle to Delphi with the expectation that Delphi would be able to directly call the routine as if it were a native Delphi routine, then that is something that is not possible -- not if the handle is with an interactive MATLAB session or with an MATLAB Compiler or MATLAB Compiler SDK compiled routine. For something like this to have a chance you would need to be using MATLAB Coder.
As yet I find no official way to convert a function handle to a generic pointer. I know that you can pass a function handle to a mex routine; see https://www.mathworks.com/matlabcentral/answers/96775-how-do-i-pass-function-handles-to-c-mex-function-in-matlab-7-8-r2009a

Arnav Mendiratta
Arnav Mendiratta 2017년 3월 3일
As long as your function handle returns a valid value, you should be able to typecast it into any kind of integer.
For example, let's define your function to return a 'double' type like this:
MyCallback = @(n) n.^2;
Now, if you call MyCallback, and notice the workspace:
>> value = MyCallback(2)
value =
4
>> whos
Name Size Bytes Class Attributes
MyCallback 1x1 32 function_handle
value 1x1 8 double
Considering your example, you can now typecast this value in your function using any of the integer classes that MATLAB supports.
In this case, you would replace the 'question mark' in your code with a typecasted value returned by function handle:
calllib('myDelphiLib','Instatiate',cParameter, uint8(MyCallback(2)) )
  댓글 수: 1
Walter Roberson
Walter Roberson 2017년 3월 3일
편집: Walter Roberson 2017년 3월 3일
What you have described is for changing the type of what is returned by invoking MyCallback(2), not for transfering the function handle itself.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by