File Exchange

image thumbnail

ENVI file reader/writer

version 1.2.0.0 (7.38 KB) by Felix Totir
Mini-toolbox for reading and writing ENVI data and header files (including complex).

29 Downloads

Updated 26 Jul 2010

View Version History

View License

%test script

%clears workspace
clearvars;

%generate a complex 3-D variable
D=rand(2,3,4)+j*rand(2,3,4);

%create basic ENVI header info about a Matlab variable
info=enviinfo(D);

%write the variable and the associated header info in a pair of binary/header ENVI files
enviwrite(D,info,'a.dat'); %implicit header file is "a.dat.hdr" (if not explicitly passed)

%read a complex n-D variable from "a.dat" and "a.dat.hdr" ENVI files
[D2,info2]=enviread('a.dat');

%some comparisons to validate consistency of our read/write procedures
isequal(D,D2)
isequal(info,info2)

%modify the header info (introducing offset in ENVI header)
info3=info2;
info3.header_offset=10000;
D3=D2; %same as D2

%writing the D3 (same as D2), but with 10000 offset
enviwrite(D3,info3,'a3.dat');

%read the offset variable (D3)
[D3a,info3a]=enviread('a3.dat');

%comparisons to validate read/write
isequal(D3,D3a)
isequal(info3,info3a)

Cite As

Felix Totir (2020). ENVI file reader/writer (https://www.mathworks.com/matlabcentral/fileexchange/27172-envi-file-reader-writer), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (26)

nlm

@Ethan Kyzivat Can you share your modifications for compatibility with me? You can send me the files to nlmmaheshwari@gmail.com

Jonathan Monsalve

Raven74

@Ethan Kyzivat Can you share your modifications for compatibility with me? You can send me the files to manea.dragos@inoe.ro

Ethan Kyzivat

Works great, but needs slight tweaks for compatibility with R2019a. I also developed a simple function that converts the output "fileinfo" structure to a matlab georeferencing structure. I am happy to send the authors these updates.

Yuean

Umit Kilic

In the previous comment, it should be Matlan R2017a not b, my mistake.

Umit Kilic

In matlan 2017b, I have this error:

Undefined function or variable 'info'.

Error in envihdrread (line 60)
if isfield(info,'map_info')

Error in enviread (line 20)
info=envihdrread(hdrfile);

Error in Untitled (line 1)
[data,information]=enviread('file.img','file.hdr');

When I check your code, couldnt find any mistake in the related functions. Anyone has the solution?

jeyson dashtabadi

jeyson dashtabadi

Giuseppe Fiore

With matlab R2015b, after modifing the script as below, envidataread,envihdrread, enviread and envidatawrite functions work good, but envihdrwrite doesn't work. It show me this error:

Error using num2str (line 45)
Input to num2str must be numeric.

Error in envihdrwrite (line 39)
line=[param,' = ',num2str(value)];

could you help me? thx

shawek

after modifiing the scripts as advised below, i'm getting an error in envihdrwrite(line39)

line=[param,' = ',num2str(value)];
Input to num2str must be numeric.

matlab R2015b
info variable looks similar to the example.

can anyone help me solve this?
thanks

sepideh abadpour

there are three bugs:
two in the file envihdrread
the line 66 in the file envihdrread which is
line=textscan(line,'%s',',');
should be changed to:
line=textscan(line,'%s','delimiter',',');
in order to be able to use the code in MATLAB R2013b
and the code:

if length(line) == 9
info.map_info.datum = line{8};
info.map_info.units = line{9}(7:end);
elseif length(line) == 11
info.map_info.zone = str2num(line{8});
info.map_info.hemi = line{9};
info.map_info.datum = line{10};
info.map_info.units = line{11}(7:end);
end
should be changed to
if length(line) == 8
info.map_info.datum = line{8};
elseif length(line) == 9
info.map_info.datum = line{8};
info.map_info.units = line{9}(7:end);
elseif length(line) == 11
info.map_info.zone = str2num(line{8});
info.map_info.hemi = line{9};
info.map_info.datum = line{10};
info.map_info.units = line{11}(7:end);
end

and another is in the envidataread file, in which we should add the code:

format = strcat('*',format);

before line 87 in order to have the output matrix in the class of the input data type
I don't know how to submit the updated form of the code?

sadrime

The line66 problem in MATLAB R-2014b could also be removed by replacing it with:

line=textscan(line,'%s','Delimiter',',');

Swadhin

Sorry, I was not able to explain fully.

Go to envihdrread.m

go to the problem line with the textscan error, in line 66.

Replace it with:

line = strsplit(line,',');

Then, remove the rest of the lines following it, until

info.map_info = [];
info.map....etc.

This has worked for me, as an alternative fix to the problem in MATLAB R-2014b

Swadhin

FOR MATLAB 2014b, REPLACE THE FOLLOWING:

line=textscan(line,'%s',','); %behavior is not quite the same if "line" ends in ','

WITH:

line = strsplit(line,',')

AND THEN, IT SHOULD WORK. IT IS WORKING FOR ME, GOOD PIECE OF CODE!

Umberto Minora

What is "string_to_cell"? Where did you find this function? It would be very useful to know since MATLAB doesn't seem to have it.

Benjamin Carlson

envihdrread.m, lines 66, 67, and 68 need to be replaced by:
line=string_to_cell(line, ',');

With this change, the program works great, thanks!
Ben Carlson, University of Oklahoma

Rob Cranfill

This looks awesome, thanks! Except, as YongHyun wrote, it seems to error out under 2014b. I will attempt to hack it into submission, and if successful, will contribute the solution here, forthwith.

I am not even sure what the line of code "line=textscan(line,'%s',',');" is trying to do - that doesn't seem to be legal syntax, AFAICT.

YongHyun

There are some errors on Ver. R2014b.

Error using textscan
Name-value pair arguments must come in pairs.

Error in envihdrread (line 66)
line=textscan(line,'%s',','); %behavior is not quite the same if "line" ends in ','

Error in enviread (line 20)
info=envihdrread(hdrfile);

Buwei

Felix Totir

@James Wheeler: thank you for your contribution. Unfortunately, I am not able to test it, but please fork the code on the MatlabCentral. Or, if others confirm and youa gree, I will integrate it into the current code (with credit).

James Wheeler

I've written a few lines for envihdrread (at line 70) to allow for projections with whitespace in them (i.e. Geographic Lat/Lon). Apologies for clumsy script, I'm new to this...
if ischar(line{2})
line{2}=[line{1},' ',line{2}];
for n=1:numel(line)-1
line{n}=line{n+1};
end
line(numel(line))=[] ;
end

Benjamin

writeenvi.m produces error: Undefined function 'fix' for input arguments of type 'struct' in num2str using version 7.14(2012A).

Felix Totir

@Jaroslaw Tuszynski: The provided function envidataread.m is esentially a wrapper around multibandread(). Even a hint towards multibandread is given in the comments on lines 88 and 90 of that function (please have a look at the code).

However, using multibandread alone requires *reading the header by a human*. The purpose of this little toolbox is exactly to automatize that operation (working with headers and data).

Jaroslaw Tuszynski

A better option is to use MATLAB's multibandread function

Margaret

Very useful for importing ENVI standard files into Matlab as well as outputing the results from a Matlab analysis back to ENVI! Runs fast - no compatibility issues with large files.

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!