matlab coder error Directly accessing field or property of nonscalar struct or object not supported for code generation.
Show older comments
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
xingxingcui
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.
Answers (1)
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
Xie Yiru
on 21 May 2019
hello I need your help about this question. I used houghpeaks() and houghlines() in matlab. But I can't transform them to C because I can not controll the size of the funciton. I can post my code.
clear
clc
close all;
oldpath = path;
path(oldpath,'E:\ZKIT_ws2017_18\Conti\Aufgabe_1')
image=imread('Kreuz_1.png');
A=neutest_mex(image);
function A = neutest_mex(n)
%coder.extrinsic('rgb2gray')
I2 = rgb2gray(n);
%coder.extrinsic('imbinarize')
I=imbinarize(I2);
theta = zeros(1,180,'double');
rho = zeros(1,381,'double');
H=zeros(381,180,'double');
%coder.extrinsic('hough')
[H,theta,rho] = hough(I);
%{
imshow(H,[],'XData',theta,'YData',rho,...
'InitialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on, axis normal, hold on;
%}
%coder.extrinsic('houghpeaks')
P = houghpeaks(H,1);
lines = struct('point1',[0,0],'point2',[0,0],'theta',0,'rho',0);
lines = repmat(line,1,0);
%coder.extrinsic('houghlines')
FillGap=double(0);
MinLength=double(0);
FillGap =double(0);
MinLength= double(0);
lines = houghlines(I,theta,rho,P,'FillGap',5,'MinLength',7);
A=lines.theta;
end
i can not use the output from the houghlines. do you know why?
Andy
on 22 May 2019
Hi Xie,
When running this through codegen "lines" ends up typed as a 1x:? struct array. As such you getting the error message about directly accessing a field of a nonscalar struct.
Could you try converting the line "A = lines.theta" into a for loop that copies each individual theta out to A? In other words change: A=lines.theta into:
A=zeros(1, size(lines,2));
for i=1:size(lines,2)
A(i)=lines(i).theta;
end
Also, I think you may have a problem with the following:
lines = struct('point1',[0,0],'point2',[0,0],'theta',0,'rho',0);
lines = repmat(line,1,0);
You pass "line" to repmat rather than "lines", "line" is a matlab function that returns an object.
This will end up causing a runtime error when you run the generated mex. You can resolve it either by commenting out this code or changing what you pass to repmat from "line" to "lines" (which is what I think you meant to do).
TripleSSSS
on 10 Jun 2020
I'd suggest using
A=coder.nullcopy(zeros(1, size(lines,2)));
instead of
A=zeros(1, size(lines,2));
Categories
Find more on MATLAB Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!