Function works fine in MATLAB, however, when I get to MATLAB coder, it fails with error does not terminate due to an infinite loop

조회 수: 1 (최근 30일)
Hi all
I've got a function I've made in MATLAB, attached to this passage.
function [undistortedImage] = imwarpingImageFromFilenamesCoder(ImageFile,movingPoints,fixedPoints) %#codegen
fid = fopen(movingPoints);
tline = fgetl(fid);
c = 2;
while(ischar(tline))
tline = fgetl(fid);
c = c + 1;
end
fclose(fid);
mP = zeros(c-2,2);
fid = fopen(movingPoints);
s = fgetl(fid);
d = {','};
l = {};
while (length(s) > 0)
[t,s] = strtok(s,d);
l = {l{:}, t};
l = string(l);
end
l = double(l);
mP(1,:) = l;
c = 2;
while(ischar(s))
s = fgetl(fid);
l = {};
while (length(s) > 0)
[t,s] = strtok(s,d);
l = {l{:}, t};
l = string(l);
end
mP(c,:) = double((l));
c = c + 1;
end
movingPoints = mP(1:end-1,:);
mP = zeros(c-2,2);
fid = fopen(fixedPoints);
s = fgetl(fid);
d = {','};
l = {};
while (length(s) > 0)
[t,s] = strtok(s,d);
l = {l{:}, t};
l = string(l);
end
l = double(l);
mP(1,:) = l;
c = 2;
while(ischar(s))
s = fgetl(fid);
l = {};
while (length(s) > 0)
[t,s] = strtok(s,d);
l = {l{:}, t};
l = string(l);
end
mP(c,:) = double((l));
c = c + 1;
end
fixedPoints = mP(1:end-1,:);
I = imread(ImageFile);
tform = fitgeotform2d(movingPoints,fixedPoints,"projective");
undistortedImage = imwarp(I,tform);
fclose(fid);
end
This function runs fine on MATLAB (sorry for how messy it is, I've been changing functions that don't work in coder with one's that do, such as readmatrix with fgetl, etc...). Now, though, it seems too run in coder (kind of) I get the source code in c, but I get an error that says this fx does not terminate due to an infinite loop. I went to the main c file, and there is indeed this line
while (1) {
fgetl(fid, c_fid);
}
Which is an infinite loop, but I don't have any idea how the MATLAB code produces this line in C, and my C skill is not the strongest, so I'm struggling to figure out how I can fix it in C.

채택된 답변

Steven Lord
Steven Lord 2023년 12월 5일
Rather than checking if s is a char vector or has positive length, if you want to stop when the file has been read in I'd use feof instead. Or since you calculate how many lines the file has (the c variable) use that in a for loop instead of using while.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 MATLAB Coder에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by