I need to use a function in Matlab that is defined by a dll file. I got an example that guy converted a dll to mexw32 file but i have known how I do this. I tried use loadlibrary but it didn't create any file.
How I can do this?
loadlibrary is MATLAB's implementation of FFI services, a mechanism of calling functions in external shared libraries. It involves converting between C-types and their equivalent MATLAB data types to pass data around.
MEX-files are also a kind of dynamically linked libraries (with .mex* extension), that can be run directly in MATLAB as a regular function without any special syntax.
The difference is that it has a specific gateway routine called mexFunction, which receives both input and output as mxArray type. mxArray is an opaque type defined in mex.h header file, which is the fundamental type underlying all MATLAB data. You usually manipulate this data using functions in the MEX library API.
Related
https://nl.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html
the article above mentions the following for MEX files.
"You can continue to use the 32-bit API by calling the mex command with the -compatibleArrayDims option"
We are calling the API functions via JNA. In other words, we are invoking functions exposed from libraries such as libmat and libmx. Is there a compatibleArrayDims option that we can use? Because it looks like this option is meant for MEX files only and we are not using MEX files
According to https://www.mathworks.com/matlabcentral/answers/99144 the signature of the implementation of the functions has changed completely.
Now, it seems the main thing is how mwIndex and mwSize are defined.
I'd guess that the -compatibleArrayDims controls some #define which sets the type of mwIndex and mwSize.
I'd also assume there is a system variable which handles that for the DLL's.
So the best way to get it is to write a question for the technical team of MATLAB.
Sorry for the noob question. I am trying to learn how to integrate a C function in Modelica model. I am having trouble understanding how to specify the path to my external functions.
I created an external C function and saved in a different directory than my model directory or the working directory. I thought that using the IncludeDirectory annotation would allow me to refer to where that C function is located, but I can't get it to work.
impure function computeHeat "Modelica wrapper for an embedded C function controller"
input Real T;
input Real Tbar;
input Real Q;
output Real heat;
external "C"annotation(Include="#include<ComputeHeat.c>",IncludeDirectory="E:/temp/source_C");
end computeHeat;
When I tried to compile a demo example model, I got the following message.
Compiler message:
Compiling and linking the model (Visual C++).
dsmodel.c
dsmodel.c(10): fatal error C1083: Cannot open include file: 'ComputeHeat.c': No such file or directory
Error generating Dymosim.
The IncludeDirectory should be an URI and only modelica-URIs are currently supported in Dymola.
So, store your function computeHeat in e.g. e:/temp/computeHeat.mo
And use IncludeDirectory="modelica://computeHeat/source_C"
In general I would assume computeHeat would be part of a package, MyPackage, stored as e:/temp/MyPackage/package.mo (and more files, e.g. computeHeat.mo).
In that case create e:/temp/MyPackage/Resources/source_C and use IncludeDirectory="modelica://MyPackage/source_C" The latter case is the recommended one, since you can copy MyPackage as a directory and the source follows.
Suppose i have a lot of source files, i want to organize them in folders-tree structure.
Is it possible for me to have several files with same name and use every of them from place i need or i must have all functions and classes with different names?
In C++ i have #include to introduce functions i need, that's here?
Just to illustrate:
.\main.m
.\Algorithms\QR\Factory.m % function Factory
.\Algorithms\QR\Algorithm.m % function Algorithm
.\Algorithms\SVD\Factory.m % function Factory
.\Algorithms\SVD\Algorithm.m % function Algorithm
MATLAB has support for namespaces. So in your example you would create the following:
C:\some\path\main.m
C:\some\path\+algorithms\+qr\factor.m
C:\some\path\+algorithms\+svd\factor.m
(Note: Only the top-level package folder's parent folder must be on the MATLAB path, i.e: addpath('C:\some\path'))
Then you could invoke each using its fully qualified name:
>> y = algorithms.qr.factor(x)
>> y = algorithms.svd.factor(x)
You could also import the package inside some scope. For example:
function y = main(x)
import algorithms.svd.*;
y = factor(x)
end
To understand the problem I need to explain some difference between the relation of c++ source and header files, and to .m files.
First: In matlab, you can only run the function which is defined highest up in the .m file. This file defines the top of the hierarchy. Then subfunctions can be implemented in the same m file, but these can only be used inside the same .m file.
Secondly: In addition to this matlab searched the include path for a specific filename and assume that the function inside the file will have the same name. You will notice this by a warning if you define the function with another name than the filename. The thing here is that you cannot have 2 matlab functions with the same name if all functions are global. This would be the same as if you would have 2 functions with the same name and in the same namespace in c++.
Note: The include path in matlab can typically be done with a hardcoded file in the to folder of your program. This function uses the matlab funcion addpath.
This is a fundamental difference to c/c++ where multiple functions are allowed to be defined in the same source file. Then the header file select what source code that you implement in the program, by providing the function definitions. The important thing here is that the header is completely disconnected from the function names, which they are not in matlab. This means that the analogy in your examples is not exactly accurate. The proposed thing by you is to "include" 2 functions with the same name. This is not possible either c/c++ (assuming the functions uses the same namespace or ar global) or in matlab.
Example: If the headers topFolder/foo/bar.h and topFolder/baz/bar.h would both contain the function void myDup(int a) and both headers uses the same namespace (or are global), then that would generate an error.
However, if the functions are only used by a limited number of other functions, then a function, eg. Factory.m, could be included as private functions in different folders. That would also mean that only this folder can access it. It is also possible to use matlab namespace as said in Amro's answer.
I am trying to use the Information Gain algorithm available in here, which is implemented in Matlab and it uses Weka java classes. However, I get the following problem when trying to run the code:
Undefined function 'wekaCategoricalData' for input arguments of type 'double'.
The code line which generates the error is this:
t.buildEvaluator(wekaCategoricalData(X, SY2MY(Y)));
SY2MY is just a transformation function and it is described here.
The algorithm apparently expects an argument of the type (spider data object), which I have no idea what it is exactly. There appears to be something wrong with the number of the arguments sent as well.
Any help is appreciated.
According the readme file included with the package, you need to run the load_fspackage.m script on initial installation of the "Feature Selection Package".
This will setup various M-file on the MATLAB path, as well as add the required Weka JAR-file to the Java classpath.
I am trying to run compiled MATLAB code (by mcc) from inside MATLAB in a way that I can avoid using another license that is required by the compiled code. We need this because we run this same specific code part again and again and execution is stuck due to license waiting. We don't want to buy tons of this specific license just to mass run the same part. Is there any way to do this? tutorial?
Is it possible to compile a .m file to dll/so and wrap it like a mex and call it from MATLAB on the fly? How would I pass and retrieve complex arguments?
According to
http://www.mathworks.de/products/compiler/description3.html
creating shared libraries should well be possible.
Concerning passing and retrieving complex arguments:
If you plan to use mex, I'd assume you should be able to call the shared-library "main"-function with any arguments you'd like, using the mxArray type that you'll have to use anyway.
To run the MATLAB-compiled code in MATLAB, you want codegen, part of the MATLAB Coder. See this blog post on generating C code from MATLAB. The alternative, deploying code with mcc/mbuild and then reloading it into MATLAB with loadlibrary is rather contorted, and I wouldn't advice it.