Error on using Mex with OpenCVLibrary in Matlab - c++

I am using a program that is written with OpenCV on Matlab. The program has been compiled and a .mexw64 file was generated.
I have added my OpenCV library to Matlab using mexopts.bat and the compile was successful.
When I call the function of the program, an error is generated:
Invalid MEX-file 'test.mexw64': test.mexw64 is not a valid Win32 application.

Make sure that the directory containing your mexw64 file is included in the MATLAB path.

Related

How to compile a cpp file with the alglib library?

I'm working on a C++ code on linux that needs some special functions which can be provided by alglib: https://www.alglib.net/download.php. The thing is, i'm new to Ubuntu so I don't know how to install the library or execute the files I need for my program.
The library's manual (https://www.alglib.net/translator/man/manual.cpp.html#gs_attaching) explain that you should just pick the packages you need and add them to your project, so I downloaded the tgz file and added specialfunctions.h, and executed it like this:
#include<math.h>
#include<stdio.h>
#include<specialfunctions.h>
But when I run the compiler through terminal I get this error:
analitica.cpp:3:9: fatal error: specialfunctions.h: No such file or directory
3 | #include<specialfunctions.h>
| ^~~~~~~~~~~~~~~~~~~~
compilation terminated.
Someone has a hint of what i'm doing wrong?

Export c++ code from simulink scheme with s-function

I have a simulink working simulink schema in which there are some s-function, I want to export this code in c++ and compile it. I already tried to export and compile a simple schema and it works, but when i try to compile the exported code of my project I get a lot of errors like
error: #error Unrecognized use
error: #error Must define one of RT,nNRT, MATLAB_MEX_FILE, SL_INTERNAL, or FIPXT_SHARED_MODULE
error: #error Unhandled case
error: ‘mxArray’ does not name a type mxArray **dlgParams; /* The S-function parameters
error: ‘mxArray’ has not been declared mxArray **)
error: ‘mxArray’ does not name a type mxArray *propVal;
error: ‘RTWSfcnInfo’ does not name a type; did you mean ‘RTWLogInfo’? RTWSfcnInfo sfcnInfo
The main settings that i used are:
Code generation system target file --> grt.tlc
language --> C++
Pack code and artifact --> <name_zip>
Tool chain GNU gcc/g++ | gmake (64-bit Linux)
default parameter behavior --> tuanble
code interface packaging --> nonreusable function
external mode --> chcked
standar math library --> c++ 03(ISO)
single output/update function --> checked
i used a fixed step size solver
Im using Ubuntu 18.
The source is generated succesfully the problem is when i try to compile the code.
Once the code is generate I get a zip folder with all the sources and also some external header file that I include when I make the CMakeLists.txt. I also tryied to compile directly the code with the .mk file but also I get a bounch of errors. The problem is realated with the s-function because I built a simple simulink schema with a source an s-function that implements a dynamical model and an output and when i generate the code and compile it I get the same errors. I also tryied to make a simple schema without s-function and this works. So is there any settings for the s-function to be exportable
At the end I found an answer that I post here in case someone else has my same problem.
The solution that worked form my is:
First of all once you have the generated zip file with all the sources and headers open it and load on the Matlab workspace the file buldinfo.mat then in the Matlab shell run
packNGo(buildInfo)
in this way you generate a new .zip with all the sources and headers, this .zip differs from the previous because is flat, no nested folder.
Inside this folder there is a file called define.txt with a list of variables, all these variables should be passed as flags to the compiler. For example in my case I use a CMakelists.txt so to set these flags I used
set(CMAKE_CXX_FLAGS "-Wall -Wextra -DMODEL=test_sum_gain -DNUMST=2 -DNCSTATES=2 -DRT -DUSE_RTMODEL -DON_TARGET_WAIT_FOR_START=0 -DCLASSIC_INTERFACE=0 -DALLOCATIONFCN=0 -DTID01EQ=1 -DEXT_MODE=1 -DMAT_FILE=0 -DONESTEPFCN=1 -DTERMFCN=1 -DMULTI_INSTANCE_CODE=0 -DINTEGER_CODE=0 -DMT=0")
this depends on the way in which you compile the source, but in any case you should set these variables that is I am not wrong are just a directive for the precompiler.
These two things worked for me.

How can I compile a mex function in Windows with Eclipse (4.20.0 version) and Matlab (R2021a)

I'm trying to compile a c++ mex function that I created according to the specifications given by mathworks here (Create a C++ MEX Source File). However, after following the steps of the answer in this thread to link Eclipse 4.4.2 and Matlab R2015a in a linux enviroment (old but the only one I've found about it), I get the following error:
undefined reference to `get_function_ptr' mpi_pevd_mex_f line 79, external location: C:\Program Files\MATLAB\R2021a\extern\include\MatlabDataArray\detail\ExceptionHelpers.hpp C/C++ Problem
I don't know what else is missing. I've been trying to find a solution, but nothing useful so far.
Thank you for your help.
I have found the problem. Apparently, I needed to add additional libraries to those detailed in most of the posts related to MEX files, i.e. libmex/ libmat/libmx.
Making use of the verbose mode in Matlab (mex -v MexFunction.cpp), I realized that these libraries were missing: libmwlapack, m, libmwblas, libMatlabEngine, libMatlabDataArray. After adding them, the error disappeared.

Fatal error LNK1168: cannot open filename.mexw64 for writing

I am writing a c++/CUDA code with Visual Studio 2015 to generate a mex file to integrate with MATLAB.
When I run the mex file through the MATLAB console and then try to compile it again on VS I get this error:
LINK : fatal error LNK1168: cannot open filename.mexw64 for writing
Closing the MATLAB and opening the programme again solves the problem.
Does anyone know any solution which not involves closing the MATLAB?
MEX-files are DLLs (on Windows, shared objects on other systems). When first run, MATLAB links to them, but doesn't unlink unless explicitly told to (or quitting MATLAB of course).
Typing
clear mex
in MATLAB will unlink all MEX-files. See the relevant documentation.
But note that your MEX-file can call mxLock, which will cause it to be locked in memory and then it will not be cleared by clear mex. If you use this function in your MEX-file, you need to include a facility to have your MEX-file call mxUnlock. I usually add a syntax such as mymexfile --unlock: the MEX-file checks to see if it is called with one argument, and that argument is a string, and the string matches "--unlock", then it calls mxUnlock.

How to call Gurobi_c++.h in a S-function of matlab Simulink?

I tried to use the header "gurobi_c++.h" to make an optimization using Gurobi Solver in Matlab but all time I get the following error:
fatal error: 'gurobi_c++.h' file not found #include "gurobi_c++.h"
I notice I already install a C++ compiler and gurobi too :
>> mex -setup
MEX configured to use 'Xcode with Clang' for C language compilation.
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the
new API. You can find more information about this at:
http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.
To choose a different language, select one from the following:
mex -setup C++
mex -setup FORTRAN
Also Gurobi is correctly installed in my Mac OS :
>> gurobi_setup
The MATLAB interface for Gurobi 7.0.2 has been installed.
The directory /Library/gurobi702/mac64/matlab/
has been added to the MATLAB path.
To use Gurobi regularly, you must save this new path definition.
To do this, type the command savepath at the MATLAB prompt. Please consult the MATLAB documentation if necessary.
>>
Thanks.