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

6 次查看(过去 30 天)
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
Yan

采纳的回答

Walter Roberson
Walter Roberson 2021-12-9
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.
  5 个评论
Walter Roberson
Walter Roberson 2021-12-10
"This structure does not have a field 'fieldName'"
My code never uses fieldName as a field name.
if the max. size of the structure is defined in pre-allocation, the line "idx = length(obj.structName.fieldNames) + 1;" should be reworked, is it right?
Right. You would instead keep a counter of the number of nodes actually in use.
num_in_use = obj.structName.num_in_use;
[~, idx] = ismember(fieldName, obj.structName.fieldNames(1:num_in_use));
if idx == 0
idx = num_in_use + 1;
if idx > length(obj.structName.nodes)
error('Node table full trying to insert "%s"', fieldName);
end
obj.structName.num_in_use = idx;
obj.structName.fieldNames{idx} = fieldName;
obj.structName.nodes(idx) = empty_node;
end
obj.structName.nodes(idx) = node;
Tianxiang Yan
Tianxiang Yan 2021-12-10
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
Yan

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by