repmat vs repelem in code generation

22 次查看(过去 30 天)
If I use repmat as below, I get the following error in code generation. Not with repelem.
Size argument must be scalar.
The above error may be reported because of a limitation rather than a true error condition. When detecting errors, complex control flow sometimes creates false positives.
Error in ==> myTest Line: 34 Column: 26
configNames.txt and configValues.txt contain a line with entries MaxTrackLim and 10 respectively.
function myTest()
%#codegen
domainID = 0;
Name = "/detectionNode";
Node = ros2node(Name, domainID);
fid_names = fopen('configNames.txt','r');
fid_values = fopen('configValues.txt','r');
row = 1;
value = fscanf(fid_values,'%f\n');
s = {''};
s{row} = fgetl(fid_names);
vnames = {'variable', 'value'};
T = table (s, value,'VariableNames',vnames);
fclose(fid_values);
fclose(fid_names);
% preallocate memory for variable array
findVar = strcmp(T.variable, 'MaxTrackLim');
MaxTrackLim = floor(T.value(find(findVar)));
detectionList = ros2publisher(Node, ...
"/obstacleList","geometry_msgs/PoseArray" ,...
"Reliability","reliable","Durability","volatile");
detectionState = ros2message("geometry_msgs/Pose");
detectionListMsg.poses = repmat(detectionState,MaxTrackLim,1);
  2 个评论
Andinet
Andinet 2023-6-23
Further point: if you replace the last line in the code with detectionListMsg.poses = repmat(detectionState,10,1); you woundn't get a code generation error.
Bruno Luong
Bruno Luong 2023-6-23
编辑:Bruno Luong 2023-6-24
Can you try with
MaxTrackLim = max([0; floor(T.value(find(findVar)))]); % edit semicolon separator since findVar could be a column vector
or
MaxTrackLim = sum(floor(T.value(find(findVar))));

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2023-6-23
编辑:dpb 2023-6-23
"configNames.txt and configValues.txt contain a line with entries MaxTrackLim and 10 respectively"
But your code contains
MaxTrackLim = floor(T.value(find(findVar)));
which is of indeterminate size; it's unknown until runtime the size find() will return -- from empty to numel(T.value) is possible.
You can try and see if
MaxTrackLim = floor(T.value(find(findVar,1)));
will be enough for the analyzer.
I'd probably rewrite the lookup table code something more on the line of
vars=readcell('configNames.txt');
vals=readmatrix('configValues.txt');
vnames = {'variable', 'value'};
T=table(vals,'VariableNames',vnames,'RowNames',vars);
MaxTrackLim=floor(T{'MaxTrackLim','variable'});
which shortens up the input code and also will handle a table of more than one variable. It will also eliminate the explicit lookup operation; whether it will be recognized as a single row or not, I don't know.
I'd also suggest to put the two separate text files together into one so they variable and the value are always together and be much easier to ensure the two match in length and intended values rather than being completely separate entities as now.
Your code is specific for just one record per file; the above would handl multiple cases/variables so especially if it is going to be so that the two files are used, it should catch that the lengths of the two are the same.
Oh...and this also presumes that readcell/readmatrix can be used by code generator, of course.
  6 个评论
dpb
dpb 2023-6-23
Looks to be worthy of reducing to the minimum to reproduce the issue and submit as bug/service report/request. If nothing else will serve as additional fodder for TMW to improve the diagnostics engine by supplying another test case.
Bruno Luong
Bruno Luong 2023-6-24
编辑:Bruno Luong 2023-6-24
In various modifications suggested by @dpb or original code, if findVar is empty MaxTrackLim is empty array. It is not a scalar in this circumstance.
IMO repelem should throw an error as well.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Matrices and Arrays 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by