transform the while loop into a loop for

4 visualizaciones (últimos 30 días)
PIERRE OLIVIER SCHOUEL ATANGANA
PIERRE OLIVIER SCHOUEL ATANGANA el 1 de Sept. de 2021
Comentada: Walter Roberson el 28 de Oct. de 2021
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

Respuesta aceptada

Image Analyst
Image Analyst el 1 de Sept. de 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 comentarios
Walter Roberson
Walter Roberson el 1 de Sept. de 2021
Editada: Walter Roberson el 1 de Sept. de 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 el 1 de Sept. de 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.

Iniciar sesión para comentar.

Más respuestas (3)

Walter Roberson
Walter Roberson el 1 de Sept. de 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
PIERRE OLIVIER SCHOUEL ATANGANA el 1 de Sept. de 2021
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 comentarios
PIERRE OLIVIER SCHOUEL ATANGANA
PIERRE OLIVIER SCHOUEL ATANGANA el 11 de Sept. de 2021
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 el 11 de Sept. de 2021
What do you want to have happen in place of the i/o operations?

Iniciar sesión para comentar.


PIERRE OLIVIER SCHOUEL ATANGANA
PIERRE OLIVIER SCHOUEL ATANGANA el 11 de Sept. de 2021
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 comentarios
PIERRE OLIVIER SCHOUEL ATANGANA
PIERRE OLIVIER SCHOUEL ATANGANA el 28 de Oct. de 2021
Editada: Walter Roberson el 28 de Oct. de 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 el 28 de Oct. de 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 ?

Iniciar sesión para comentar.

Categorías

Más información sobre Model Statistics en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by