Clear Filters
Clear Filters

Why am I getting invalid file identifier error?

18 views (last 30 days)
This section of code that I am running keeps giving me the error "Invalid file identifier. Use fopen to generate a valid file identifier" when it gets to the parfor loop. I cannot figure out what would be causing the issue. Where is my file identifier invalid?
if loc1(1)>loc2(1)
disp('start from flood tide')
name = {'/home/kward/model/particles/flood/particles_','/home/kward/model/particles/ebb/particles_'};
else
disp('start from ebb tide')
name = {'/home/kward/model/particles/ebb/particles_','/home/kward/model/particles/flood/particles_'};
end
loc= unique([loc1,loc2]);
t_r = (t(loc(1:end-1))+t(loc(2:end)))/2;
name1 = name{1};
name2 = name{2};
parfor i=1:length(t_r)
if mod(i,2) == 1
OF = [name1,num2str((i+1)/2,'%02d'),'.dat'];
else
OF = [name2,num2str(i/2,'%02d'),'.dat'];
end
fprintf('Saving %s\n',OF)
if (exist(OF, 'file')==2)
delete(OF);
end
fid = fopen(OF,'wt');
fprintf(fid, [num2str(length(lon)*num),' 2\n']);
fprintf(fid, 'testing\n');
fprintf(fid, 'vertical coordinate = zx y z t_release\n');
fclose(fid);
tsi = [lon,lat,lat,lat]; % longtitude&latitude
tsi(:,3) = 0; % depth
tsi(:,4)=t_r(i);%time
tsi = repmat(tsi,num,1);
disp('Writing data...');
dlmwrite(OF, tsi, 'delimiter', ' ', '-append','precision','%.4f');
end

Answers (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov on 25 Nov 2022
Hi,
I would check the directory address is correct. And if it is correct, then if MATLAB is reading it correctly.
name = {'/home/kward/model/particles/flood/particles_','/home/kward/model/particles/ebb/particles_'};

Raymond Norris
Raymond Norris on 25 Nov 2022
fopen will return two output arguments: the file id and an error message if it failed (i.e., the file id is <0). Add the following for additional diagnostics
[fid, emsg] = fopen(OF,'wt');
if fid<0
error(emsg)
end

Categories

Find more on Java Package Integration in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!