简体   繁体   English

为什么我突然在MATLAB中收到无效的文件标识符错误?

[英]Why I am suddently getting invalid file identifier error in MATLAB?

Code: 码:

list_of_files=dir(fullfile('/home/username/Desktop/Old/MTP/tfcodev2p1/data/', '*.dat'));

There are 50 dat files in that folder. 该文件夹中有50个dat文件。 Weird thing is it was working perfectly alright till last night. 奇怪的是,直到昨天晚上,它的运行情况还不错。 Now I am suddenly getting this error. 现在,我突然收到此错误。 Error: 错误:

Error using fseek
Invalid file identifier.  Use fopen to generate a
valid file identifier.

Error in getvar>get_convar (line 162)
fseek(f,8*mesh.n*(sim.nvars*t+VARS) , 'bof');

Error in getvar (line 82)
  data=get_convar(fname,sim,mesh,VARS,t);

Error in realn (line 34)
       D1=getvar(list_of_files(i).name,sim,mesh,'rhoi',sim.nsaves);

I am working on Ubuntu 12.04. 我正在使用Ubuntu 12.04。 I have tried putting my data in different folder but am still getting the same error. 我尝试将数据放在其他文件夹中,但仍然遇到相同的错误。

Here is the code. 这是代码。 File realn.m: 文件realn.m:

list_of_files=dir(fullfile('/home/rishi/Desktop/Old/MTP/tfcodev2p1/data', '*.dat'));
%simulation details
%display(list_of_files.name);
hold off;
sim.tend=0.1;
sim.gamma=5.0/3.0;
sim.nsaves=1;
sim.nvars=20;
sim.dim=1;
sim.mass_ratio=1.0;
%mesh details
mesh.nx=100; %number of cells in x-direction
mesh.ny=1; %number of cells in y-direction
mesh.maxx=1.0;
mesh.maxy=0;
mesh.n=mesh.nx*mesh.ny;
mesh.x=linspace(0,mesh.maxx,mesh.nx);

N=100;
n=51;
tau=0.0001;
h=1/N;
lambda=tau/h;
mu=lambda/2;
uf=zeros(n,1);
uf1=zeros(n,1);
uf2=zeros(n,1);
phi=zeros(n,1);
for i=1:n
    phi(i,1)=2*pi*i/N;    
end
for i=1:length(list_of_files)
   display(list_of_files(i).name)
       D1=getvar(list_of_files(i).name,sim,mesh,'rhoi',sim.nsaves);
       D2=getvar(list_of_files(i).name,sim,mesh,'rhoi',0);
   for j=1:N
       uf1(i,1)=uf1(i,1)+(D1(j)*exp(-1i*j*phi(i)));
       uf2(i,1)=uf2(i,1)+(D2(j)*exp(-1i*j*phi(i)));
   end
   uf1(i,1)=uf1(i,1)/100;
   uf2(i,1)=uf2(i,1)/100;
end

getvar.m getvar.m

function data=getvar(fname,sim,mesh,V,t)
 NVARS=36;
 VARS=0;
 //switch upper(V)


 if (VARS<sim.nvars)
  data=get_convar(fname,sim,mesh,VARS,t);
 elseif(VARS<NVARS)
  data=get_extravar(fname,sim,mesh,VARS,t);
 else
    display('WRONG Variable: Check function getvars()')
 end

function data=get_extravar(fname,sim,mesh,VARS,t)

switch VARS
    case {20}
        mxe=get_convar(fname,sim,mesh,6,t);
        data=-mxe/sim.mass_ratio;    
    case {21}
        mye=get_convar(fname,sim,mesh,7,t);
        data=-mye/sim.mass_ratio;            
    case {22}
        mze=get_convar(fname,sim,mesh,8,t);
        data=-mze/sim.mass_ratio;      
    case {23}
        mxi=get_convar(fname,sim,mesh,1,t);
        data=mxi; %assuume that m_i=1.0 
    case {24}
        myi=get_convar(fname,sim,mesh,2,t);
        data=myi; %assuume that m_i=1.0 
    case {25}
        mzi=get_convar(fname,sim,mesh,3,t);
        data=myi; %assuume that m_i=1.0 
    case {26}
        rhoe=get_convar(fname,sim,mesh,5,t);
        mxe=get_convar(fname,sim,mesh,6,t);
        data=mxe./rhoe;    
    case {27}
        rhoe=get_convar(fname,sim,mesh,5,t);
        mye=get_convar(fname,sim,mesh,7,t);
        data=mye./rhoe;
    case {28}
        rhoe=get_convar(fname,sim,mesh,5,t);
        mze=get_convar(fname,sim,mesh,8,t);
        data=mze./rhoe;
    case {29}
        rhoi=get_convar(fname,sim,mesh,0,t);
        mxi=get_convar(fname,sim,mesh,1,t);
        data=mxi./rhoi;
    case {30}
        rhoi=get_convar(fname,sim,mesh,0,t);
        myi=get_convar(fname,sim,mesh,2,t);
        data=myi./rhoi;
    case {31}
        rhoi=get_convar(fname,sim,mesh,0,t);
        mzi=get_convar(fname,sim,mesh,3,t);
        data=mzi./rhoi;     
    case {32}
        mxe=get_convar(fname,sim,mesh,6,t);
        mye=get_convar(fname,sim,mesh,7,t);
        mze=get_convar(fname,sim,mesh,8,t);
        data=sqrt(mxe.^2 + mye.^2 + mze.^2);
    case {33}
        mxi=get_convar(fname,sim,mesh,1,t);
        myi=get_convar(fname,sim,mesh,2,t);
        mzi=get_convar(fname,sim,mesh,3,t);
        data=sqrt(mxi.^2 + myi.^2 + mzi.^2);
    case {34}
        mze=get_convar(fname,sim,mesh,8,t);
        mzi=get_convar (fname,sim,mesh,3,t);
        data=abs(mzi-mze/sim.mass_ratio);   
   case {35}
       mxe=get_convar(fname,sim,mesh,6,t);
       mye=get_convar(fname,sim,mesh,7,t);
       mze=get_convar(fname,sim,mesh,8,t);

        mxi=get_convar (fname,sim,mesh,1,t);
        myi=get_convar (fname,sim,mesh,2,t);
        mzi=get_convar (fname,sim,mesh,3,t);
        data=abs(mxi + myi + mzi-(mxe+mye+mze)/sim.mass_ratio);        
end

function data=get_convar(fname,sim,mesh,VARS,t)
f=fopen(fname,'r');
if (VARS<sim.nvars)
fseek(f,8*mesh.n*(sim.nvars*t+VARS) , 'bof');
if (sim.dim==1)
data=fread(f,[mesh.nx],'float64');
elseif(sim.dim==2)
    data=fread(f,[mesh.ny,mesh.nx],'float64');
else
     error('Check Dimension');
end
else
    error('Variable is out of range Use function get_extravar')
end
data=data';
fclose(f);

Only thing I can say to help here (I cannot reproduce the problem, obviously) is to use this line from help fopen : 我只能说要在这里提供帮助的事情(显然,我无法重现该问题)是使用help fopen这一行:

[FID, MESSAGE] = FOPEN(FILENAME,...) returns a system dependent error message if the open is not successful. 如果打开失败,则[FID,MESSAGE] = FOPEN(FILENAME,...)返回与系统有关的错误消息。

Plant this in your get_convar function, and look at the MESSAGE . 将其get_convar您的get_convar函数中,然后查看MESSAGE That should give you at least a hint as to what could be going on. 那至少应该给您一个提示,说明可能会发生什么。

Please keep me posted, as I'm always looking for new ways in which file I/O can get messed up, to better ruggedize my own file reading/writing routines :) 请随时通知我,因为我一直在寻找可以弄乱文件I / O的新方法,以更好地增强我自己的文件读取/写入例程:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM