MATLAB Answers

Pointer to Simulink Function

27 views (last 30 days)
Carlos Ma
Carlos Ma on 21 Feb 2020
Edited: Carlos Ma on 24 Feb 2020
Suppose I have a custom C function that accepts a function pointer as an argument,
float cFunc(float val, float (*ptrFunc)(float)) {
return ptrFunc(val);
}
and within Simulink, I defined a Simulink Function called "slxFunc" that takes a float as an input and outputs a float.
How do I obtain the pointer to slxFunc so that I could feed it to cFunc using a C-caller block?
Much appreciated, thanks.
/* edit 1: extra info */
The following C code is what I am trying to emulate,
#include <stdio.h>
/* times2 should be defined in simulink */
float times2(float val) {
return (2.0*val);
}
/* times3 should also be defined in simulink */
float times3(float val) {
return (3.0*val);
}
/* myFunc is defined in a .c file */
float myFunc(float val, float (*slxFunc)(float)) {
return (slxFunc(val));
}
int main()
{
/* printf should print 4.0 6.0 */
printf("%f\t%f\n", myFunc(2.0, times2), myFunc(2.0, times3));
return 0;
}
, where
times2
and
times3
are to be defined in the Simulink environment, and
myFunc
is implemented as a custom C function.

  2 Comments

stozaki
stozaki on 23 Feb 2020
can you use stateflow?
Carlos Ma
Carlos Ma on 24 Feb 2020
Thanks for replying @stozaki.
I'd really love to get it working in stateflow too, but it returned an unresolved data error.
Is there a way I could rectify this?

Sign in to comment.

Answers (1)

stozaki
stozaki on 24 Feb 2020
I attahced example model, hedder and souce C file.
Please try to simulate there.

  3 Comments

Carlos Ma
Carlos Ma on 24 Feb 2020
Perhaps we have a misunderstanding here, your example runs fine but it doesn't describe what I am trying to implement. I have no problem passing pointers to variables, but am having a problem with passing a pointer to a function inside Simulink.
The following C code is what I am trying to emulate,
#include <stdio.h>
/* times2 should be defined in simulink */
float times2(float val) {
return (2.0*val);
}
/* times3 should also be defined in simulink */
float times3(float val) {
return (3.0*val);
}
/* myFunc is defined in a .c file */
float myFunc(float val, float (*slxFunc)(float)) {
return (slxFunc(val));
}
int main()
{
/* printf should print 4.0 6.0 */
printf("%f\t%f\n", myFunc(2.0, times2), myFunc(2.0, times3));
return 0;
}
, where
times2
and
times3
are to be defined in the Simulink environment, and
myFunc
is implemented as a custom C function.
Thanks again.
stozaki
stozaki on 24 Feb 2020
Blocks provided by Simulink do not have the concept of pointers.
Can you use the newly added model?
Carlos Ma
Carlos Ma on 24 Feb 2020
Thanks for your help but again that's not what I am looking for. I have no problem calling C functions either using C Caller / Stateflow. The problem lies in passing a pointer to a Simulink function to a predefined C function, as described by the line
printf("%f\t%f\n", myFunc(2.0, times2), myFunc(2.0, times3));
If you parse this line in a generic c compiler, printf would return 4.0 and 6.0 as expected as I can pass the functions times2 and times3 into the function myFunc. The above line does not compile, however, in Stateflow with C being selected as the action language, as Simulink expects an argument for times2 and times3 but doesn't realise that I am trying to pass the functions are function pointers instead.
Thanks again if you have any ideas.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!