how can i control a global variable in "C" file which is shared between different functions and are called using code.ceval
조회 수: 6 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
Maneet Kaur Bagga
2024년 2월 15일
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:
Persistent variable: https://www.mathworks.com/help/matlab/ref/persistent.html
Hope this helps!
댓글 수: 6
Maneet Kaur Bagga
2024년 2월 19일
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!
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!