matlab 如何解决“代码生成不支持直接访问非标量结构或对象字段或属性”?

np8igboo  于 2023-04-06  发布在  Matlab
关注(0)|答案(1)|浏览(434)

代码如下:

function [allLinePoint] = create_hough_quadprog_b(BW) %#codegen
%UNTITLED3 Summary of this function goes here
%   Detailed explanation goes here
[H,theta,rho] = hough(BW,RhoResolution=5);

P = houghpeaks(H,30,'Threshold', 0.1*max(H(:)) );
lines = houghlines(BW,theta,rho,P,'FillGap',60,'MinLength',5 );

pointX = vertcat(lines.point1); %% <---- error here
pointX = vertcat(lines.point1);
pointY = vertcat(lines.point2);
allLinePoint = cat(1,pointX,pointY);
end

编码器命令:

codegen -config:lib -args {zeros(3000,3000,'logical')} create_hough_quadprog_b

错误信息:

Directly accessing field or property of nonscalar struct or object not supported for code generation in this context.

我的问题是:我应该用for-loop替换这一行来逐个访问结构体属性吗?

8mmmxcuj

8mmmxcuj1#

我的问题是:我应该用for-loop替换这一行来逐个访问结构体属性吗?
是的,你应该这样做,截至目前,最新版本的MATLAB R2023 a仍然不支持上述语法/功能,可以直接使用for循环逐个赋值,例如,支持以下代码片段。

numsLines = numel(lines);
allLinePoint = coder.nullcopy(zeros(2*numsLines,2, "double"));
for idx = 1:numsLines
    allLinePoint(idx,:) = lines(idx).point1;
    allLinePoint(idx+numsLines,:) = lines(idx).point2;
end

相关问题