MATLAB Answers

transform the while loop into a loop for

6 views (last 30 days)
Hello ,
I would like to turn the following block of code into a loop for
while feof(fid) == 0
tline = fgetl(fid);
TargetVar = regexp(tline,' ','split');
if length(TargetVar)>2
[xc, reste] =strtok(tline, ' ');
[yc, reste] =strtok(reste, ' ');
[zc, reste] =strtok(reste, ' ');
[Rayon, reste] =strtok(reste, ' ');
h=h+1;
Pts(h,1:3)= [str2num(xc) str2num(yc) str2num(zc)] ;
Ray(h,1)=str2num(Rayon);
Vol(h,1)=((4/3)*pi)*(Ray(h)^3);
end
end

Accepted Answer

Image Analyst
Image Analyst on 1 Sep 2021
Try this:
maxLines = 9999999; % More than you expect to ever need.
for k = 1 : maxLines
tline = fgetl(fid);
if ~ischar(textLine)
% End of file so break out of loop.
break;
end
% The rest of your code follows (I didn't check it).
TargetVar = regexp(tline,' ','split');
if length(TargetVar)>2
[xc, reste] =strtok(tline, ' ');
[yc, reste] =strtok(reste, ' ');
[zc, reste] =strtok(reste, ' ');
[Rayon, reste] =strtok(reste, ' ');
h=h+1;
Pts(h,1:3)= [str2num(xc) str2num(yc) str2num(zc)] ;
Ray(h,1)=str2num(Rayon);
Vol(h,1)=((4/3)*pi)*(Ray(h)^3);
end
end
  3 Comments
Image Analyst
Image Analyst on 1 Sep 2021
I agree that while is preferable. I just gave the for loop way because I guess he wanted it for comparison purposes.
Often people know how big their files are. Like if you know you're writing data for up to hundred files and each file will have 5 numbers for it, then in your output text file you won't have more than 100 lines in it and it won't be hundreds of GB. The comment says that you put the line count at way more than you know you'll ever need, and in that case, the code works.
maxLines = 9999999; % More than you expect to ever need.

Sign in to comment.

More Answers (3)

Walter Roberson
Walter Roberson on 1 Sep 2021
for is only for use with a fixed number of iterations, but your code has no inherent limits on the number of lines it reads from a file.
In theory you could be running MATLAB on Linux or MacOS with a ZFS file system, which permits invididual files as large as 2^64 - 1 bytes. If you transform the while into a for you would have to loop
for LINE = 1:inf
but MATLAB constrains the number of iterations for a for loop to be at most flintmax('double') -- 2^54 . So you would have to use nested loops to get the rest of the potential iterations before you ran into the potential file size limit.

PIERRE OLIVIER SCHOUEL ATANGANA
Hello,
please how to parallelize matlab code which has a for included in a break. eg:
maxLines = 9999999
for k = 1 : maxLines
tline = fgetl(fid);
if ~ischar(textLine)
% End of file so break out of loop.
break;
end
%there are all instructions below
end
  13 Comments
Walter Roberson
Walter Roberson on 11 Sep 2021
What do you want to have happen in place of the i/o operations?

Sign in to comment.


PIERRE OLIVIER SCHOUEL ATANGANA
In fact the context is that of the calculation of the ellipsoid from the coordinates of each ball. the block that I want to parallelize is the one that grouping balls from the coordinates.
When I change the for to parfor these 02 instructions cause me problems
  12 Comments
PIERRE OLIVIER SCHOUEL ATANGANA
thank you for this solution track,
I will implement it and report to you

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!