Reading in binary data/variables
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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
0 Kommentare
Antworten (1)
Guillaume
am 19 Nov. 2014
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 Kommentare
Guillaume
am 19 Nov. 2014
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.
Siehe auch
Kategorien
Mehr zu Large Files and Big Data finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!