function [fh, swapbyteorder]=MATOpen(filename, permission)
% MATOPEN opens a MAT file in the appropriate endian mode.
% Example:
% [FH, SWAP]=MATOPEN(FILENAME, PERMISSION)
% FH is the returned file handle
% PERMISSION is the permission string for FOPEN
% SWAP is set true if the byte order is different to the default of the host
% platform
%
% Author: Malcolm Lidierth 09/06
% Copyright Kings College London 2006
% Revisions:
[platform,maxsize,system_endian] = computer;
fh=fopen(filename,permission,system_endian);
if fh<0
swapbyteorder=[];
disp(sprintf('MATOPEN: Failed to open %s',filename));
return;
end
fseek(fh,0,'bof');
level=fread(fh,4,'uint8');
if level(1)==0 || level(1)==0 || level(2)==0 || level(3)==0
disp('MATOPEN: unsupported Level 4 MAT-file format');
fclose(fh);
fh=-1;
swapbyteorder=[];
return;
end
fseek(fh,114+10,'bof');
level=fread(fh,1,'uint16=>uint16');
if level==512
disp('MATOPEN: unsupported Level 7 MAT-file format: -v7.3 switch used on save');
swapbyteorder=[];
fclose(fh);
fh=-1;
return;
end
endian=fread(fh,1,'uint16=>uint16');
switch endian
case 18765
fclose(fh);
switch system_endian
case 'L'
fh=fopen(filename,permission,'B');
case 'B'
fh=fopen(filename,permission,'L');
end
swapbyteorder=true;
case 19785
swapbyteorder=false;
otherwise
fclose(fh);
fh=-1;
disp('MATOPEN: could not determine file byte order.');
end