Reading in binary file leads to empty matrix

2 Ansichten (letzte 30 Tage)
x y
x y am 30 Aug. 2015
Bearbeitet: Walter Roberson am 31 Aug. 2015
I have a binary file ('test') together with a header file ('test.hdr') which I am trying to import into matlab. I can read out the information out of the header file, however I am struggling to read in the binary file. Here you can download the two files I am trying to import.
This is my code so far:
% read in header file
[fid message]=fopen('test.hdr','rt');
if(~isempty(message))
error(message);
return;
end
while(~feof(fid)) % Repeat the following command sequence until EOF is reached
textline=fgetl(fid);
[nameAndValue] = regexp(textline,'=','split');
fieldName = strtrim(nameAndValue{1});
value = strtrim(nameAndValue{2});
if(~isempty(str2num(value)))
value = str2num(value);
else
value = strtok(value,'''');
end
header.(fieldName) = value;
end
fclose(fid);
% read in binary
[fid message]=fopen('test','wb','ieee-be');
if(~isempty(message))
error(message);
return;
end
How could I read in the data, the header contains informatio about the data type and the dimensions of the matrix. When trying to read in data like this I get an empty matrix A.
A=fread(fid);
How could I read in this data with using the header information? The binary file is exported from ENVI and in big-endian byte order.

Akzeptierte Antwort

Image Analyst
Image Analyst am 30 Aug. 2015
If you are trying to open a file for reading, then why are you opening it for writing?
[fid message]=fopen('test','wb','ieee-be');
'wb' means write binary. I think you really want rb like this:
[fid message]=fopen('test','rb','ieee-be');
I also second Geoff's comment about the filename. You should use fullfile() to create a filename with the full folder, base filename, and extension. Then use exist(fullFileName, 'file') to see if it actually exists first before calling fopen().
  1 Kommentar
Walter Roberson
Walter Roberson am 30 Aug. 2015
Bearbeitet: Walter Roberson am 31 Aug. 2015
Earlier you spoke of 'test' and 'test.hdr', but now you are trying to open 'topomap'
If I use your earlier code together with your later
A = fread(fid, [180,360], 'float32');
then the data looks okay when you use
imagesc(A,'XData',[180,-180], 'YData',[-90,90]); axis xy

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

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!

Translated by