Converting Matlab function to mex with user defined class object

I need to create a mex file of a function. One of the arguments of the function is the matlab user defined class object. When I try to convert the function to mex, it gives me an error that the function input does not have valid type and user defined class is not supported by coder.type. Is there any solution to this?

5 件のコメント

Adam
Adam 2014 年 11 月 3 日
I think you can convert the class to a struct to retain its properties, but lose its functions which are obviously not transferable to mex.
I can't remember off-hand how to do this though so you could probably search for that as fast as I could!
Guillaume
Guillaume 2014 年 11 月 3 日
To convert an object to a struct:
s = struct(obj);
Adam
Adam 2014 年 11 月 4 日
Oh yeah...that was too obvious to remember!
Amit
Amit 2014 年 11 月 11 日
Thanks Adam and Guillaume. DO you know any simpler method to convert structure back to user defined matlab class object? I can assign manually all the properties of the object with structure but as there are more that 50 properties it is tedious and perhaps will make simulation slower again.
Guillaume
Guillaume 2014 年 11 月 11 日
Answer to this question, in the answers below

サインインしてコメントする。

 採用された回答

Ryan Livingston
Ryan Livingston 2014 年 11 月 4 日
編集済み: Ryan Livingston 2017 年 12 月 17 日

0 投票

Edit: As of MATLAB R2017a value objects can be inputs and outputs from MEX files.
In earlier releases, MATLAB Coder does not support the input or output arguments of the MEX function being MATLAB objects. One other option you have is to pass the values to the MEX function needed to construct the object and construct the object inside the function being passed to codegen. So rather than:
function y = foo(myObj)
<code>
you could use:
function y = foo(arg1,arg2,arg3,)
myObj = MyClass(arg1,arg2,arg3);
<code>
Then you can use the object as before in the code and still generate MEX from the function.
Using the struct approach, you could pass in the struct and then re-create the object from the struct inside of the function.

1 件のコメント

Matt J
Matt J 2023 年 9 月 12 日
Edit: As of MATLAB R2017a value objects can be inputs and outputs from MEX files.
Are there any relevant documentation links with examples that you could provide?

サインインしてコメントする。

その他の回答 (1 件)

Guillaume
Guillaume 2014 年 11 月 11 日
編集済み: Guillaume 2014 年 11 月 11 日

0 投票

To convert a structure back to an object you can do the following function
function object = propfromstruct(object, s)
for fieldname = fieldnames(s)'
try
o.(fieldname) = s.(fieldname);
catch ex
warning('could not copy field %s due to error %s', fieldname, ex.message);
end
end
end

1 件のコメント

Dalia Sobhy
Dalia Sobhy 2017 年 12 月 14 日
編集済み: Dalia Sobhy 2017 年 12 月 14 日
Hello
It is not working

サインインしてコメントする。

製品

質問済み:

2014 年 11 月 3 日

コメント済み:

2023 年 9 月 12 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by