matlab coder error Directly accessing field or property of nonscalar struct or object not supported for code generation.

71 views (last 30 days)
This works in Matlab but not in Coder, Why?
s1=string({OPS_FLT(:).ACFT_ID})
s2=OPS_FLT(2).ACFT_ID
uuindex=find(strcmpi(s1,s2))
My array is defineded as:
OPS_FLT(1).ACFT_ID="apple"
OPS_FLT(2).ACFT_ID="orange"
I need to find orange from the array but the code has to be compatible in coder. It says: Directly accessing field or property of nonscalar struct or object not supported for code generation in ({OPS_FLT(:).ACFT_ID})
  1 Comment
cui
cui on 6 Apr 2023
It is hoped that future versions will support this feature, for example, the current R2023a supports "end+1" C/C++ code generation, which would greatly reduce the concerns of developers.

Sign in to comment.

Answers (1)

Andy
Andy on 14 Jan 2019
Edited: Andy on 16 Jan 2019
Hi Upol. Presently, referencing a component from an array of non-scalar values (accessing a field in a struct array) is not allowed in code generation.
As a workaround, for the time being you can rewrite the code to avoid the non-scalar access:
function uuindex = foo
OPS_FLT = struct('ACFT_ID', {"apple", "orange"});
uuindex=[];
for k = numel(OPS_FLT)
if OPS_FLT(k).ACFT_ID == OPS_FLT(2).ACFT_ID
uuindex = [uuindex, k];
end
end
end
*EDIT: I've modified code from version I original posted since it used another feature that also would not compile.
  3 Comments

Sign in to comment.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by