Why am I getting invalid file identifier error?

27 ビュー (過去 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 Center および File ExchangeJava Package Integration についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by