Why am I getting invalid file identifier error?

조회 수: 25 (최근 30일)
Kelsey Ward
Kelsey Ward 2022년 11월 25일
답변: Raymond Norris 2022년 11월 25일
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

답변 (2개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2022년 11월 25일
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 2022년 11월 25일
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

카테고리

Help CenterFile Exchange에서 Java Package Integration에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by