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 view (last 30 days)
Show older comments
Alexander Dumont
on 5 Dec 2023
Commented: Alexander Dumont
on 6 Dec 2023
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.
0 Comments
Accepted Answer
Steven Lord
on 5 Dec 2023
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.
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!