calling cvx function in c++ by using matlab library compiler, but i met some problem with the global variable global cvx___,can anyone help me?
8 次查看(过去 30 天)
显示 更早的评论
calling cvx function in c++ by using matlab library compiler, but i met some problem with the global variable global cvx___,can anyone help me?
in c++ , i call the cvx function interface, but error happened.(no error in matlab)
%%%%%%
Dot indexing is not supported for variables of this type.
Error in cvx_global (line 77)
Error in cvxprob (line 4)
Error in cvx_begin (line 41)
Error in optFunc (line 14)
%%%%%%
and i found that maybe the global variable "global cvx___", in matlab the variable "cvx___.solvers" is not empty,
but in c++ the variable "cvx___.solvers" is empty. I don't know why,can anyone help me? thanks
also, i found that someone has the similar puzzle , they want to use cvx in a standalone .exe or java.
link: https://www.mathworks.com/matlabcentral/answers/398959-stand-alone-matlab-exe-cannot-call-functions-successfully
0 个评论
回答(1 个)
Namnendra
2024-10-30,4:37
Hi,
When using the MATLAB Compiler to call MATLAB functions from C++, certain global variables and states may not be preserved or initialized correctly, leading to errors like the one you're experiencing with CVX. Here's a detailed approach to troubleshoot and potentially resolve this issue:
1. Ensure Proper Initialization:
- CVX relies on some initialization steps that are usually done when you first run a CVX program in a MATLAB session. Make sure that the initialization steps are included in your compiled application.
- Before calling any CVX functions, ensure that you have executed any necessary startup scripts or initialization commands that set up global variables and solvers. This might include running `cvx_setup` if it’s applicable.
2. Check CVX Compatibility:
- Verify that the version of CVX you are using is compatible with the MATLAB Compiler. Some toolboxes or third-party libraries might not fully support compilation or have known issues when used in compiled applications.
3. Modify MATLAB Code:
- If possible, modify the MATLAB code to avoid reliance on global variables. You can refactor the code to use input/output arguments to pass necessary data, reducing the dependency on global state.
- Consider encapsulating the CVX setup and problem-solving logic within a single function call that ensures all necessary components are initialized.
4. Use Persistent Variables:
- Instead of global variables, use persistent variables within functions if you need to maintain state. Persistent variables are generally more robust in compiled applications.
5. Debugging:
- Add debugging statements in your MATLAB code to print out the state of variables before and after key operations. This can help identify where the state diverges between MATLAB and the compiled C++ application.
- Use `fprintf` or other logging mechanisms to capture the state of `cvx___.solvers` and related variables.
6. MATLAB Compiler Restrictions:
- Be aware of any restrictions or limitations of the MATLAB Compiler with respect to the toolboxes and functions you are using. Check the documentation for any notes specific to CVX or similar optimization libraries.
By following these steps, you should be able to better understand and potentially resolve the issues with using CVX in a compiled C++ application.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 C Shared Library Integration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!