Reading in binary file leads to empty matrix

2 ビュー (過去 30 日間)
x y
x y 2015 年 8 月 30 日
編集済み: Walter Roberson 2015 年 8 月 31 日
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.

採用された回答

Image Analyst
Image Analyst 2015 年 8 月 30 日
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 件のコメント
Walter Roberson
Walter Roberson 2015 年 8 月 30 日
編集済み: Walter Roberson 2015 年 8 月 31 日
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

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLarge 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