enviLoadHeader.m
1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
function s = enviLoadHeader(filename)
%create a cell array of fields
s = struct;
fid = fopen(filename, 'r', 'n', 'US-ASCII');
%read the first field and make sure it is "ENVI"
fname = GetFieldName(fid);
if strcmp(fname, 'ENVI') == 0
disp('Not an ENVI header file');
return;
end
while feof(fid) == 0
fname = GetFieldName(fid);
if feof(fid) == 1
return;
end
[value, valid] = ReadField(fid, fname);
if valid == 1
s = setfield(s, fname, value);
end
end
fclose(fid);
function t = GetFieldName(fid)
string_struct = textscan(fid, '%s', 1, 'Delimiter', '=');
if feof(fid) == 1
t = [];
return;
end
t = string_struct{1}{1};
t = strtrim(t);
t(t==' ') = '_';
function [v, valid] = ReadField(fid, field_name)
valid = 1;
stringFields = {'file_type', 'interleave', 'sensor_type', 'wavelength_units'};
intFields = {'samples', 'lines', 'bands', 'header_offset', 'data_type', 'byte_order'};
%if the field is "description", read between the brackets
if strcmp(field_name, 'description') == 1
textscan(fid, '%[{]', 1);
string_struct = textscan(fid, '%[^}]', 1, 'Whitespace', '');
textscan(fid, '%[}]', 1);
v = string_struct{1}{1};
v = strtrim(v);
return;
end
if max(strcmp(field_name, intFields)) ~= 0
v = fscanf(fid, '%d');
return;
end
if max(strcmp(field_name, stringFields)) ~= 0
string_struct = textscan(fid, '%s', 1, 'Whitespace', '\n');
v = string_struct{1}{1};
v = strtrim(v);
return;
end
%read and return the wavelength values
if strcmp(field_name, 'wavelength') == 1
v = [];
textscan(fid, '%[{]', 1);
c = ' ';
while c ~= '}'
new = fscanf(fid, '%f');
v = [v new];
c = fscanf(fid, '%c', 1);
end
return;
end
%if it doesn't match anything, just read until the end of the line
%string_struct = textscan(fid, '%s', 1, 'Whitespace', '\n');
string_struct = textscan(fid, '%s', 1, 'Delimiter', '\n');
v = '';
valid = 0;