Reading in binary data/variables

조회 수: 3 (최근 30일)
Jeff
Jeff 2014년 11월 19일
댓글: Guillaume 2014년 11월 19일
Hello,
I wrote a function that should be able to read in a binary data file (reading in the appropriate variables and using them to calculate temperature), but I keep running into problems. My code does not want to read in the variables -- it keeps generating the following error:
"Error using fread Operation is not implemented for requested file identifier.
Error in get_temp (line 27) fread(nx)"
If anyone has any suggestions I would greatly appreciate it! My code is based on my co-workers IDL version, but I need to get a MATLAB version of it working (and unfortunately I am not too familiar with using MATLAB). Below is the code. Thanks in advance!
function get_temp
% Variables for reading
nx = 0;
ny = 0;
nz = 0;
zlength = 0.;
abs_power = 0.;
Tmin = 0.;
Tmax = 0.;
sampleRadius = 0.;
sampleThickness = 0.;
opticalThickness = 0.;
absorberFlag = 0;
absorberRadius = 0.;
absorberThickness = 0.;
laserMode = 0;
laserWaist = 0.;
laserPower = 0.;
k0 = 0.;
% Open file for reading
fid = fopen('test.dat');
F = fread(fid);
% Read in header info
fread(nx)
%ny, nz, zlength, absorberFlag, absorberRadius, absorberThickness, sampleThickness, %sampleRadius, opticalThickness, abs_power, Tmin, Tmax, laserMode, laserWaist, laserPower, k0)
% Create arrays to store data
x = single(nx);
y = single(ny);
z = single(nz);
T = single(nx*ny*nz);
% Read in temperature data and convert to Kelvin
% fread T
T = T*(Tmax-Tmin) + Tmin;
% Done with file; close it
fclose all
% Create position arrays
h = 1./(nz-2);
for i=0,nx-2
x(i+1) = (1.*i+0.5)*h
end
for j=0,ny-2
y(j+1) = (1.*j+0.5)*h
end
for k=0,nz-2
z(k+1) = (1.*k-0.5)*h
end
% Remove outer cells and dimensionalize position arrays to microns
nxSmall = nx-2
nySmall = ny-2
nzSmall = nz-2
TSmall = T(1:nxSmall,1:nySmall,1:nzSmall)
zlength = zlength*1.e6
xSmall = x(1:nxSmall)*zlength - zlength/2.
ySmall = y(1:nySmall)*zlength - zlength/2.
zSmall = z(1:nzSmall)*zlength - zlength/2.
return
end

답변 (1개)

Guillaume
Guillaume 2014년 11월 19일
The error message pretty much tells you everything. I don't think you understood the syntax of fread. The first argument of fread is a file identifier, you're passing it nx which you've declared as 0.
I don't understand your code at all. You're opening the file fine, then read the entire content with
F = fread(fid);
At this point there's nothing left to read, so you might as well close the file there and then. The whole content of the file is in F and you never do anything with it.
Then there's the
fread(nx)
which will never work. Maybe you intended
nx = fread(fid, 1, ?); %replace ? by adequate precision
but since you've already read the whole content in the first place, there's nothing to read anyway.
I think you need to explain better what you're trying to do.
Finally don't use fclose all to close the file. That's a sledgehammer approach. Use
fclose(fid);
  댓글 수: 2
Jeff
Jeff 2014년 11월 19일
What I am trying to do is read in the variables (nx, ny, nz... laserPower, K0) from the binary file. In IDL I know the command to do so is readu (which reads unformatted binary data from a file into IDL variables). I made the assumption that fread was the equivalent of this, but based on what I have read it is a little more complex than just using fread....
Guillaume
Guillaume 2014년 11월 19일
Most likely, your IDL uses strongly typed variables, so it can just infer how many bytes to read from the variable. You can't do that in matlab, so you need to know beforehand what type of variable you want to read.
Were your nx, ny, etc. all the same type and what type was that?
You also have to watch for endianness ( machinefmt argument of fread).
Hopefully you have the documentation for the binary format. It should detail all of this.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by