repmat vs repelem in code generation

20 views (last 30 days)
Andinet
Andinet on 23 Jun 2023
Edited: Bruno Luong on 24 Jun 2023
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 Comments
Andinet
Andinet on 23 Jun 2023
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 on 23 Jun 2023
Edited: Bruno Luong on 24 Jun 2023
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))));

Sign in to comment.

Answers (1)

dpb
dpb on 23 Jun 2023
Edited: dpb on 23 Jun 2023
"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 Comments
dpb
dpb on 23 Jun 2023
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 on 24 Jun 2023
Edited: Bruno Luong on 24 Jun 2023
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.

Sign in to comment.

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!