how can i control a global variable in "C" file which is shared between different functions and are called using code.ceval

6 views (last 30 days)
Assume the following C code. i just tried to generalise the code as my i need.
/* C Code*/
//declaration of global variable
int flag = 0;
//function 1 definition
temp_func1 ()
{
flag = 1
}
//Function 2 definition
temp_func2(int *ptr)
{
if (flag == 1)
{
*ptr = 10;
}
else
{
*ptr=0;
}
}
//Matlab function
global temp_act;
coder.ceal('temp_func1');
coder.ceal('temp_func2',coder.ref(temp_act));
The abvoe matlab function is always returning 0 which means that the global variable flag updated in a function temp_func1 is not getting reflected in temp_func2. i confirmed this by removing the condition (flag==1) from the C code and successful in getting the value 10 from the matlab function. Appreciating your support.

Answers (1)

Maneet Kaur Bagga
Maneet Kaur Bagga on 15 Feb 2024
Hi,
As per my understanding, you want to manage a global variable when interfacing between MATLAB and C code using the "coder.ceval" function.
One of the possible ways to do this is, since MATLAB may not directly manage the state of C global variables, providing accessor functions in C to explicitly get and set the value of the flag will avoid potential issues with the global variable's state not being recognized across different function calls. In MATLAB, you would then use "coder.ceval" to call these accessor functions:
% MATLAB function
global temp_act;
coder.ceval('temp_func1');
coder.ceval('set_flag', 1); % Explicitly set the flag to 1
coder.ceval('temp_func2', coder.ref(temp_act));
Another approach to address this is to use "persistent memory" in MATLAB to store and manage the state of the global variable. This method avoids direct manipulation of the C global variable and encapsulates the state within the MATLAB environment and uses pass-by-reference to share the state with the C functions. Please refer to the below modifications required in the C and MATLAB code for using this approach.
"C code" modification: Remove the global variable "flag" from the C code and modify the functions to accept a pointer to the flag as an argument.
/* C Code */
// Function 1 definition
void temp_func1(int *flag) {
*flag = 1;
}
// Function 2 definition
void temp_func2(int *flag, int *ptr) {
if (*flag == 1) {
*ptr = 10;
} else {
*ptr = 0;
}
}
"MATLAB Code" modification: Use a "persistent" variable in MATLAB to hold the state of the flag and pass it to the C functions as needed.
% MATLAB function
persistent flag;
if isempty(flag)
flag = 0; % Initialize the persistent variable
end
temp_act = 0; % Initialize the output variable
% Call the C functions with the persistent variable
coder.ceval('temp_func1', coder.ref(flag));
coder.ceval('temp_func2', coder.ref(flag), coder.ref(temp_act));
% Now temp_act should have the correct value based on the state of flag
Please refer to the MATLAB documentation for further understanding:
Hope this helps!
  6 Comments
Suthakar Muthuganesan
Suthakar Muthuganesan on 19 Feb 2024
Thanks for your response.
i did the following,
1) i have included the mexfunction in C file and successfully generated the mex file followed by the linked library created in the form of .dll.
2) The dll is included using the SimUserLibraries option and it is now included in the model configuration parameter.
3) the model will still try to invoke the C-function using coder.ceval. i did try to compile the model and got an issue that the model is unable find the definition of the C-function.
4) how can i check the presence of the C-function in the created mex or dll ? this is just to double check the conversion.
is there anything more to be taken care?
appreciating your support!!
Maneet Kaur Bagga
Maneet Kaur Bagga on 19 Feb 2024
Hi,
To solve this you need to ensure that the MEX file and the DLL are correctly being referenced and that the C-function is properly exported and accessible.
Check the presence of C-function in a MEX File
Use "mex" command of MATLAB : If you have compiled the C code into a MEX file, you can test the MEX function directly in MATLAB by calling it like any other MATLAB function. If the MEX function works as expected in MATLAB, it confirms the presence and accessibility of the C-function.
Check the presence of C-function in a DLL
Use Dependency Walker : On Windows, Dependency Walker (depends.exe) is a free utility that scans any 32-bit or 64-bit Windows module (DLL, EXE, etc.) and builds a hierarchical tree diagram of all dependent modules. You can open the DLL with Dependency Walker to see if the C-function is listed as an exported function.
Ensure that the DLL is correctly referenced
Also, please ensure that the DLL is in the sytem path, or the path to the DLL is specified in MATLAB using the "addpath" function or by configuring it in the model settings under the "SimUserLibraries" option.
Hope it helps!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!