Commit abdb93f5939f8013588eb192837e4e8f0125d82b
1 parent
3d0b6243
added code for saving ENVI files in matlab
Showing
1 changed file
with
66 additions
and
0 deletions
Show diff stats
1 | +%loads an ENVI file without any manipulation (changing orientation) | |
2 | +function enviSaveRaw(M, filename, headername) | |
3 | + | |
4 | + %if a header isn't provided, assume it's just the filename | |
5 | + % with '.hdr' added to the end | |
6 | + if nargin == 2 | |
7 | + headername = [filename '.hdr']; | |
8 | + end | |
9 | + | |
10 | + %open a file for writing | |
11 | + fid = fopen(filename, 'w'); | |
12 | + | |
13 | + %write the data to disk | |
14 | + fwrite(fid, M, class(M)); | |
15 | + | |
16 | + %close the file | |
17 | + fclose(fid); | |
18 | + | |
19 | + %open a header file for writing | |
20 | + fid = fopen(headername, 'w'); | |
21 | + fprintf(fid, 'ENVI\n'); | |
22 | + fprintf(fid, 'description = {}\n'); | |
23 | + fprintf(fid, 'samples = %d\n', size(M, 1)); | |
24 | + fprintf(fid, 'lines = %d\n', size(M, 2)); | |
25 | + fprintf(fid, 'bands = %d\n', size(M, 3)); | |
26 | + fprintf(fid, 'header offset = 0\n'); | |
27 | + fprintf(fid, 'file type = ENVI Standard\n'); | |
28 | + | |
29 | + %get a string representing the matlab data type | |
30 | + matlab_real = isreal(M); | |
31 | + | |
32 | + if(isa(M, 'uchar')) | |
33 | + envi_type = 1; | |
34 | + elseif(isa(M, 'short')) | |
35 | + envi_type = 2; | |
36 | + elseif(isa(M, 'int')) | |
37 | + envi_type = 3; | |
38 | + elseif(isa(M, 'single') && matlab_real) | |
39 | + envi_type = 4; | |
40 | + elseif(isa(M, 'double') && matlab_real) | |
41 | + envi_type = 5; | |
42 | + elseif(isa(M, 'single') && ~matlab_real) | |
43 | + envi_type = 6; | |
44 | + elseif(isa(M, 'double') && ~matlab_real) | |
45 | + envi_type = 9; | |
46 | + elseif(isa(M, 'ushort')) | |
47 | + envi_type = 12; | |
48 | + elseif(isa(M, 'ulong')) | |
49 | + envi_type = 13; | |
50 | + elseif(isa(M, 'int64')) | |
51 | + envi_type = 14; | |
52 | + elseif(isa(M, 'uint64')) | |
53 | + envi_type = 15; | |
54 | + end | |
55 | + | |
56 | + fprintf(fid, 'data type = %d\n', envi_type); | |
57 | + | |
58 | + fprintf(fid, 'interleave = bsq\n'); | |
59 | + fprintf(fid, 'sensor type = Unknown\n'); | |
60 | + fprintf(fid, 'byte order = 0\n'); | |
61 | + fprintf(fid, 'x start = 0\n'); | |
62 | + fprintf(fid, 'y start = 0\n'); | |
63 | + fprintf(fid, 'wavelength units = Unknown\n'); | |
64 | + fprintf(fid, 'z plot titles = {Unknown, Unknown}\n'); | |
65 | + fprintf(fid, 'pixel size = {1, 1, units=Meters}\n'); | |
66 | + fclose(fid); | |
0 | 67 | \ No newline at end of file | ... | ... |