transform the while loop into a loop for

2 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
Walter Roberson
Walter Roberson on 1 Sep 2021
Edited: Walter Roberson on 1 Sep 2021
This is not equivalent code. It will give out after roughly
lines = (9999999+1);
chars_per_line = 50;
max_file_size_estimate_gigabytes = lines * chars_per_line / 2^30
max_file_size_estimate_gigabytes = 0.4657
I can store files 1000 times larger than that on my system, without difficulty.
Only transform while loops into for loops if you have a maximum number of iterations (or fixed number of iterations): if you cannot put an upper bound on the number of iterations, then leave it as a while loop.
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
PIERRE OLIVIER SCHOUEL ATANGANA
Please never another worry too,
How do I ignore I / O instructions (write, read, write to file, and read to file) in a MATLAB program?
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
  16 Comments
PIERRE OLIVIER SCHOUEL ATANGANA
Edited: Walter Roberson on 28 Oct 2021
Good evening,
Echan = [Echan2; Echan];
It is a matrix which contains the coordinates of each ball. And a ball is defined by three coordinates x, y and z
Walter Roberson
Walter Roberson on 28 Oct 2021
You do not use the information in Echan to draw the balls.
The information you accumulate in Echan has different coordinates than what you draw with (unless by coincidence.)
The reason I bother mentioning this is that if you are not going to use Echan to draw the balls, then there is no point in building that information and you can throw away a number of lines. Doing that would remove any need for us to worry about what the sizes of various arrays are.
But if you need to use those coordinates to draw the balls, and you want to vectorize it all, then we have to know size() of each variable involved. For example is size(phi) guaranteed to be the same as size(theta) ? Is phi a column vector and theta is a row vector? Are they scalars ?

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!