array of system objects in Matlab - matlab

Is it possible to create an array of system objects in Matlab and then mex into C code?
For example, in C++ you can create vector where myClass is a user defined class. Is it possible to create the equivalent thing in Matlab using system objects and then build to C code?

Creating ordinary arrays of System objects is not supported. You can place them in cell arrays. But code generation to C code or mex file does not support any array of classes. If you can give more detail on what you are trying to do with array of objects we can try to see whether there are alternate approaches to solve your problem.

Related

Size limit in MATLAB MEX-files

I'm new to Matlab mex. I'm trying to write a mex function which in turn will take the structure data from a .cpp file and use it in Matlab.
I came to know that plhs and prhs are the pointers to an array which hold the output data and input data respectively, each element of type mxArray.
Since structure size can be large, Is there any maximum size limit for the plhs and prhs array to hold the data ? If so, What are the alternate ways?
As far as I've researched the size of arrays that can be handled depends on the API you compile your mex files with. You can choose the API by adding the corresponding flag in your compiling instruction. The details are in the matlab documentation under "api-release specific API".
There are 4 options avaliable: -R2017b (default) -R2018a -largeArrayDims and -compatibleArrayDims.
In term of array size -R2017b (default) -R2018a and -largeArrayDims use the Large-array-handling API which according to the matlab mex documentation can handle arrays over 231-1 and, according to the API documentation should be able to handle arrays up to 248-1 elements and sparse arrays up to 248-2.
Only the last option, -compatibleArrayDims won't handle arrays above 231-1
Apart from array size those options will change the way a few data types are handled, noticeably complex types and graphics object.
So in short:
-R2017b (default) : 248-1 elements per array
-R2018a : 248-1 elements per array
-largeArrayDims : 248-1 elements per array
-compatibleArrayDims 231-1 elements per array
Finally if you want to handle larger object the solution I see would be to write your results in files (.txt or .csv for example) in the c-part of your code and read them back in the matlab part in whole or in chunks.

Matlab Array with Same name Name

I am writing some Matlab code that I am parsing into c++. The C++ Looks like this:
ICOMPL[dataPath].Value =5;
How to write this in Matlab so that the syntax is similar? For example;
ICOMPL = [0,1,2,3];
Let's me do
ICOMPL(datapath+1) = 5;
But how to add the value part? I would need that each element in the array would have the name value.
Note that MATLAB is not C++ so not everything has to be the same, specially when you get to objects. My answer assumes that ICOMPL is not an object from a class, but an struct:
You can make structs in MATLAB, also arrays of structs.
A struct is as easy as
ICOMPL.Value= 5;
An array of structs:
ICOMPL(datapath+1).Value = 5;
So each of ICOMPL will be a whole struct. Note that you may not need this, and you may want to have
ICOMPL.Value= 1:5;
A single struct with several values on each of its elements. Often this last one is easier to work with in MATLAB. That's your decision to make.

MATLAB Coder giving out-of-bounds error for struct

I am trying to MEX some code by using MATLAB's coder toolkit. The code initially had cell arrays in it which is not handled by the coder at the moment, so I decided to use structs in compensation for that.
My issue is that the size of the struct is not fixed, and herein lies the problem. What I have essentially is this:
Temp= struct('a',"some variable");
for i = 2:x
Temp(j).('a') = Temp(i-1).('a')*Temp(1).('a');
end
In the command window of MATLAB, this would be completely acceptable, however when trying to build the MEX file, it throws this error:
Index expression out of bounds. Attempted to access element 2. The valid range is 1-1.
Is there a way to fix this, or is there another solution to 'cell array' like structures that the coder will allow?
You can use repmat:
MyStruct = repmat(Temp,1,N);
where N is a constant (i.e. hard-coded, not data-dependent).
Then, if you wish,
for i=2:N
MyStruct(i).a = MyStruct(i-1).a*MyStruct(1).a;
end
No need for MyStruct(i).('a')

S function for 2 D look up Table, Similar to "sfix_look1_dyn.mex32"

I need some information about "sfix_look1_dyn.mex32" file. Currently I am using this mex file, with the help of masked s function block. I am using this block only for 1 D look up related calculations. But if I want to use similar kind of logic for 2 D Look up table then is it possible to use same mex file ? Basically I want to know is there any way out so that, for 2 D lookup I can give values of Table Data as Inputs.
AFAIK you can't use same mex file or tlc for 2-D lookup, its for Dynamic lookup 1D Block
However, you can use n-D Lookup Table block

Setting the value of an array control of a Labview VI through ActiveX (with Matlab)

I have a Labview VI that I intend to run from Matlab through ActiveX. It has one argument (that is, one Labview control), which is of type 1D numeric array.
The method used to call the VI from Matlab through ActiveX is detailed in a previous post.
I am trying to set the value of this array control in Matlab before running the VI (that is, Matlab will pass an argument to the VI and then run it; no action is to be performed manually through the Labview interface).
Getting the value through the GetControlValue method works fine (I get a nice Matlab array). However, when I try to set the value of this same control with SetControlValue using the value returned by GetControlValue, the value of the control becomes empty (as evident from the value Empty matrix: 1-by-0 obtained by Matlab after using GetControlValue again, and in Labview where the values of the control become grayed-out).
The same procedure works perfectly when the control is a single numeric value.
What is going wrong here exactly ?
See the screen capture below:
You can compile the VI to a DLL and call your function that way. This abstracts away LabVIEW's typesystem and its COM runtime.
Can you provide more detail about the problem you are trying to solve?
(Source: 0utlaw on the NI forum).
A useful workaround to this problem is to use a Matrix control in Labview.
Matlab can then pass usual arrays, and Labview maps these Matlab arrays to the matrix. Works as expected with 2D arrays as well.