필터 지우기
필터 지우기

repmat vs repelem in code generation

조회 수: 14 (최근 30일)
Andinet
Andinet 2023년 6월 23일
편집: Bruno Luong 2023년 6월 24일
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에서 Large Files and Big Data에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by