Invalid file identifier in Parfor loop

How to correct this error when using the parfor loop? Any suggestion to solve this issue? Thanks.
Invalid file identifier. Use fopen to generate a valid file identifier.
filename01 = "D:\...\testParfor"; % filename01 file parth
parfor j = 1:nk2_node
% File path
filename_HD = sprintf('%s%s%d',filename01,"\zone_case0\profile_",j);
Cal_cont = HD_Nod_Inf(filename_HD);
...
end

9 Comments

Error using HD_Nod_Inf (line 7)
Invalid file identifier. Use fopen to generate a valid file identifier.
function Cal_water_cont = HD_Nod_Inf(filename_HD)
% Read the values in "Nod.out" file
filename_Nod_sub = sprintf('%s%s',filename_HD,"\Nod.out");
fidin_Nod_sub=fopen(filename_Nod_sub,'r+');
Line_nod_sub=0;
while ~feof(fidin_Nod_sub)
Line_nod_sub=Line_nod_sub+1;
newtline_Nod_sub{Line_nod_sub}=fgetl(fidin_Nod_sub);
end
fclose(fidin_Nod_sub);
for i=1:199
k = i + Line_nod_sub-1-199;
Cal_cont(i,1:22) = str2num(newtline_Nod_sub{k});
end
end
We recommend that you use fullfile() rather than string concatenation to build file names.
filename_Nod_sub = fullfile(filename_HD, "Nod.out");
and
filename_HD = fullfile(filename01, "zone_case0", "profile_"+j);
Error using HD_Nod_Inf (line 8)
Invalid file identifier. Use fopen to generate a valid file identifier.
The code still does not wrok for "while ~feof(fidin_Nod_sub)"
function ...
while ~feof(fidin_Nod_sub) % Invalid file identifier. Use fopen to generate a valid file identifier.
...
end
Change
fidin_Nod_sub=fopen(filename_Nod_sub,'r+');
to
[fidin_Nod_sub, msg] = fopen(filename_Nod_sub,'r+');
if fidin_Nod_sub < 0
error('Could not open file "%s" because "%s"', filename_Nod_sub, msg);
end
I test this funtion seperately, it works well. But the errors occurs ... in parfor loop.
Error using HD_Nod_Inf (line 22)
Array indices must be positive integers or logical values.
function Cal_water_cont = HD_Nod_Inf(filename_HD)
...
for i=1:199
k = i + Line_nod_sub-1-199;
Cal_cont(i,1:22) = str2num(newtline_Nod_sub{k}); % error
end
...
end'
That code assumes that you were able to read at least 399 lines from the file, and that you want to skip the first 200 lines.
If that is what you are trying to do, then I would suggest you replace your current function, to become:
fopen() like before
fmt = repmat('%f', 1, 22);
Cal_cont = cell2mat(textscan(fidin_Nod_sub, fmt, 'HeaderLines', 200));
fclose(fidin_Nod_sub)
No, I wanted to read the last 199 lines from the end of file (excpet the last line).
============
Line(1)
...
Line( n)
Line (n+1)
...
Line (n +199)
Line 200
for i=1:199
k = i + Line_nod_sub-1-199;
Cal_cont(i,1:22) = str2num(newtline_Nod_sub{k}); % error
end
Suppose that 50 lines were read, so Line_nod_sub = 50. First iteration of the loop, i is 1, and k = 1 + 50 - 1 - 199 which is 51-200 which is -149 . Out of range.
Suppose that 205 lines were read, so Line_nod_sub = 205. First iteration of the loop, i is 1, and k = 1 + 205-1-199 = 6, which is okay. Last iteration of the loop, i is 199, and k = 199 + 205 - 1 - 199 = 204, which looks okay.
So I guess your loop might be okay -- but not until after you enforce that Line_nod_sub is at least 200.
Yes, I also think so.
The reason why the code does not work is that the exe fails to run the model. So there is no output values in that file.
Thanks for your help and time.

Sign in to comment.

Answers (0)

Products

Release

R2021b

Asked:

on 10 Mar 2022

Commented:

on 11 Mar 2022

Community Treasure Hunt

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

Start Hunting!