Containers.Map and struct with dynamic field names not supported in code generation

1 view (last 30 days)
Hello there,
Now I have finished a hybrid A* Planner in Matlab and would like C++ code generation. (though there is plannerhybridastar embedded in Matlab.) In this planner is containers.Map used for open set. But the coder said it does not support.
It’s not cool but I am not so surprised. I try the workaround with struct array. The field name is the ID of nodes and the value is node (instantiated object). With “isfield” it can check, whether a ID in struct exist. It works in Matlab
but again the code generation fails with error:
Non-constant expression or empty matrix. This expression must be constant because its value determines the size or class of some expression.
at following:
obj.structName.(fieldName)= node;
% assign “node” to field name in structName
In which fieldName is a variable with type char and will be updated. After a small study the reason should be, Coder does not support accessing the fields of a structure using dynamic field names.
But in my opinion, it is necessary that “fieldName” as dynamic, because it is unknown and will be assigned.
Now I am confused and do not know if there is workaround or solution. Actually, what I need is something which is supported by code generation and can store key and value accordingly. As above, the value is instantiated object.
Do you have some ideas and could you help me? Thanks in advance.
Best Regards

Accepted Answer

Walter Roberson
Walter Roberson on 9 Dec 2021
Struct array. One of the fields is the node ID, and the other is the node value. Instead of checking whether the node exists using isfield(), check whether the key is present in the node id list.
For efficiency, use a tree strategy, or at least a sorted list that you can do a binary search on. Or a hash if you expect a lot of them.
Use an external library if you like: the external data structure could take in IDs and the "value" associated could be the linear index into the struct array.
Tianxiang Yan
Tianxiang Yan on 10 Dec 2021
Thank you for the patience and cool idea. It works! and I have also learned, it is necessary to pre-allocate the struct in constructor.
Best regards

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!