Error using fgets Invalid file identifier. Use fopen to generate a valid file identifier. Error in fgetl (line 34) [tline,lt] = fgets(fid);

36 次查看(过去 30 天)
I get the following errors:
Error using fgets
Invalid file identifier. Use fopen to generate a valid file identifier.
Error in fgetl (line 32)
[tline,lt] = fgets(fid);
Error in Nemoh (line 105)
ligne=fgetl(fid);
Error in nemoh_run (line 21)
[A,B,Fe]=Nemoh(w,dir,depth);
So I run nemoh_run.m which calls Nemoh.m and then I end up with the errors above.
nemoh_run.m
function nemoh_run()
clear all;
w=(0.2:0.1:2.0);
dir=0.0;
depth=20;
[A,B,Fe]=Nemoh(w,dir,depth);
end
Nemoh.m
%
% --> function [A,B,Fe]=Nemoh(w, dir, depth)
%
% Purpose: Matlab wrapper for calculation of hydrodynamic coefficients using Nemoh
%
% Inputs :
% - w : Vector length(w) of wave frequencies (rad/s)
% - dir : Wave direction (degrees)
% - depth : water depth (m), 0 for deep water.
%
% Outputs :
% - A : Matrix (6xnBodies)x(6xnBodies)xlength(w) of added mass coefficients
% - B : Matrix (6xnBodies)x(6xnBodies)xlength(w) of radiation damping coefficients
% - Fe : Matrix (6xnBodies)xlength(w) of exciation forces (complex
% values)
%
% Copyright Ecole Centrale de Nantes 2014
% Licensed under the Apache License, Version 2.0
% Written by A. Babarit, LHEEA Lab.
%
function [A,B,Fe]=Nemoh(w, dir, depth)
% Preparation du calcul
fid=fopen('ID.dat');
line=fgetl(fid);
rep=fscanf(fid,'%s',1);
fclose('all');
fid=fopen([rep,filesep,'Nemoh.cal'],'r');
for i=1:6
ligne=fgetl(fid);
end
nBodies=fscanf(fid,'%g',1);
fclose(fid);
fid=fopen([rep,filesep,'Nemoh.cal'],'r');
n=1;
clear textline;
textline={};
while (~feof(fid))
textline(n)={fgetl(fid)};
if (n == 4)
textline(n)={sprintf('%f ! DEPTH ! M ! Water depth',depth)};
end
if ((mod(n,18) == 9) && ((n-9)/18 <= nBodies))
temp=cell2mat(textline(n));
temp2=[];
ntemp=length(temp);
k=1;
for i=1:ntemp
if (temp(i) == '\')
temp2=[temp2,temp(k:i),'\'];
k=i+1;
end;
end
temp2=[temp2,temp(k:ntemp)];
textline(n)={temp2};
cell2mat(textline(n));
end
if (n == 9+18*nBodies)
textline(n)={sprintf('%g %f %f ! Number of wave frequencies, Min, and Max (rad/s)',length(w),w(1),w(length(w)))};
end
if (n == 10+18*nBodies)
textline(n)={sprintf('%g %f %f ! Number of wave directions, Min and Max (degrees)',1,dir,dir)};
end
n=n+1;
end
fclose(fid);
fid = fopen([rep,filesep,'Nemoh.cal'], 'w');
for i=1:n-1
fprintf(fid, [cell2mat(textline(i)),'\n']);
end
fclose(fid);
fid=fopen([rep,filesep,'input.txt'],'wt');
fprintf(fid,' \n 0 \n');
status=fclose(fid);
% Calcul des coefficients hydrodynamiques
l = isunix;
if l == 1
fprintf('\n------ Starting NEMOH ----------- \n');
system('preProc');
fprintf('------ Solving BVPs ------------- \n');
system('Solver');
fprintf('------ Postprocessing results --- \n');
system('postProc');
else
fprintf('\n------ Starting NEMOH ----------- \n');
system('.\Nemoh\preProcessor.exe');
fprintf('------ Solving BVPs ------------- \n');
system('.\Nemoh\Solver.exe');
fprintf('------ Postprocessing results --- \n');
system('.\Nemoh\postProcessor.exe');
end
%% Lecture des resultats CA CM Fe
clear Periode A B Famp Fphi Fe;
fid=fopen([rep,filesep,'Nemoh.cal'],'r');
for i=1:6
ligne=fgetl(fid);
end
nBodies=fscanf(fid,'%g',1);
for i=1:2+18*nBodies
ligne=fgetl(fid);
end
nw=fscanf(fid,'%g',1);
fclose(fid);
fid=fopen([rep,filesep,'results',filesep,'ExcitationForce.tec'],'r');
ligne=fgetl(fid);
for c=1:6*nBodies
ligne=fgetl(fid);
end;
ligne=fgetl(fid);
for k=1:nw
ligne=fscanf(fid,'%f',1+12*nBodies);
w(i)=ligne(1);
for j=1:6*nBodies
Famp(k,j)=ligne(2*j);
Fphi(k,j)=ligne(2*j+1);
end;
end;
status=fclose(fid);
fid=fopen([rep,filesep,'results',filesep,'RadiationCoefficients.tec'],'r');
ligne=fgetl(fid);
for i=1:6*nBodies
ligne=fgetl(fid);
end;
for i=1:nBodies*6
ligne=fgetl(fid);
for k=1:nw
ligne=fscanf(fid,'%f',1+12*nBodies);
for j=1:6*nBodies
A(i,j,k)=ligne(2*j);
B(i,j,k)=ligne(2*j+1);
end;
ligne=fgetl(fid);
end;
end;
status=fclose(fid);
% Expression des efforts d excitation de houle sous la forme Famp*exp(i*Fphi)
i=sqrt(-1);
Fe=Famp.*(cos(Fphi)+i*sin(Fphi));
end
  3 个评论
Walter Roberson
Walter Roberson 2022-10-2
It will be several hours before I am back to my desktop to test with.
in the meantime I recommend that you rewrite your code using fullfile() instead of using the string concatenation that you are using. And make sure that the output directory exists before trying to create a file there. And always check whether fopen succeeded.

请先登录,再进行评论。

采纳的回答

dpb
dpb 2022-10-2
Indeed, just downloaded and unzipped into a clean subdirectory -- it isn't the failure to open ID.dat that you're seeing, it is that the "results" output directory that is identifed as the last line in that file as the target output location doesn't exist -- and the code as written neither checks that the fopen succeeds nor creates the needed directory if it doesn't -- it just crashes and burns.
To continue with the least effort, just create the missing subdirectory but as @Walter suggests, the better solution would be to update the code to be less user belligerent in adding error checking and informative diagnostics on failure and also create the requested output directory if it doesn't exist -- or at least ask the user before exiting.
The use of fullfile would be highly recomended, use; it would handle the case of a file separator being in the input directory string besides being more legible coding practice.
As another coding practice noticed -- remove the "clear all" from your function run_nemoh; it serves at all inside a function and is exceedingly dangerous should that get turned into a script and executed. While it is an unfortunate pedagogical crutch used, is seems almost universally in classes/uni settings, it is virtually never needed and should be avoided owing to its side effects on cache, etc., etc., unless theres is a known specific reason for it.
  4 个评论
dpb
dpb 2022-10-3
编辑:dpb 2022-10-3
fid = fopen([rep,filesep,'Nemoh.cal'], 'w');
is also using "rep" subdirectory (which was read from the ID.dat file as the string "Results" as the location for the file Nemoh.cal.
By default, unzipping the attached zip file leaves both ID.dat and Nemoh.cal in the nemoh2 directory along with the two m files. It would seem if it is an input file putting it in the Results directory is somewhat unusual unless that is what the original creator chose to do to keep input and output together for a given analysis.
Anyway, to make it work as is, move "Nemoh.cal" to the "Results" subdirectory and joy should ensue...the changes suggested would still be good and would have provided the klew as to what was actually happening...
dpb
dpb 2022-10-3
编辑:dpb 2022-10-3
ADDENDUM:
Once the above fix is made, it proceeds but will run into a similar problem expecting two more (missing, not just location) files -- this time, though, it hardcodes yet another layer of subdirectories --
fid=fopen([rep,filesep,'results',filesep,'ExcitationForce.tec'],'r');
adds a second "results" subdirectory below the one referenced by the string in variable rep earlier. And, it expects there to be content in this file to begin with.
Similarly, just a few more lines down past that one finds
fid=fopen([rep,filesep,'results',filesep,'RadiationCoefficients.tec'],'r');
in the same location also being read.
There are no files with a ".tec" file extension in the zip file; you'll have to create or find those somewhere.
And, it appears there's a header or other content at the beginning of these files that is skipped before reading the numeric data, so you'll have to figure out the format there (or hopefully have some examples to use a guides or there is a user reference somewhere that describes the file structure/content).
Similar constructs as Walter illustrated(*) above would be good modifications to make around these as well, of course, as well as consideration of just what the directory structure should look like; creating the second hardcoded layer underneath the top one seems peculiar and a lot of bother to me, but I don't have any idea what is actually going on with the application, of course, that might explain the "why" behind the original choice.
What it would appear by this is that there was a specific user-set directory and then another built under it for possibly different cases. But why the first or this would be "results" and have input is still a mystery.
(*) I might suggest this could be a good place/time to replace this external file (ID.dat) getting the input location with a call to uigetdir to let the user select the desired directory and then pass that returned location instead...it's a little more mod to the original program, but would make it a lot more user friendly...

请先登录,再进行评论。

更多回答(1 个)

Avikash Chand
Avikash Chand 2022-10-5
I actually found a workaround to this problem (This doesnt solve the problems existimg atm). I did notice there was a file missing from my folder (mesh.cal) that I found in another sample Folder I got from Github, so when I copied my code to that sample folder, the code ran as it is now, the program worked and I got the solutions I was looking for ..... The missing file could have been the issue but who knows. But then I didnt know how to modify the mesh.cal file for my case so I got stuck there.
So long story short, I was able to run the program through cli after creating the required files with BEMROSETTA .... so What BEMROSETTA did is basically create all the necessary input files with correct configs for running the program through the cli. I got the bat file which I executed to get the program (The Nemog program is also provided as an .exe file) to run. This workaround didnt involve the use of MATLAB.
I didnt try the solutions given here as I got the program running via CLI 2 days after I asked here and also coz I was pressed for time. Thanks for all your answers @Walter Roberson and @dpb. Cheers!!

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by