function [DataMat,FilePath,SizeDataMat,NDim,Version]=ReadNDimData(FilePath,Format,FilterSpec,DialogTitle,SizeStatus);

%   ReadNDimData

%   This software is protected by german copyright and international treaties.             
%   Copyright 2004 Markus Junghöfer & Peter Peyk. All Rights Reserved.                     
%                                                                                          
%   THIS SOFTWARE AND DOCUMENTATION IS PROVIDED "AS IS," AND COPYRIGHT HOLDERS MAKE        
%   NO REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO,    
%   WARRANTIES OF MERCHANTABILITY OR FITNESS FOR ANY PARTICULAR PURPOSE OR THAT THE USE    
%   OF THE SOFTWARE OR DOCUMENTATION WILL NOT INFRINGE ANY THIRD PARTY PATENTS, COPYRIGHTS,
%   TRADEMARKS OR OTHER RIGHTS. COPYRIGHT HOLDERS WILL NOT BE LIABLE FOR ANY DIRECT,       
%   INDIRECT, SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF ANY USE OF THE SOFTWARE      
%   OR DOCUMENTATION.  

if nargin<5;SizeStatus=0;end;
if nargin<4;DialogTitle=[];end;
if nargin<3;FilterSpec=[];end;
if nargin<2;Format='float32';end;
if nargin<1;FilePath=[];end;

if isempty(DialogTitle)
	DialogTitle='Read MRI data from file:';
end
if isempty(FilterSpec)
	FilterSpec='*';
end
if isempty(Format)
	Format='float32';
end
[DefFilePath]=SetDefPath('r',FilterSpec);
[File,Path,FilePath]=ReadFilePath(FilePath,DefFilePath,DialogTitle);
if File==0; DataMat=[]; FilePath=[]; SizeDataMat=0; NDim=0; Version=[]; return; end
fprintf('Read data from file...\n\n');
disp(FilePath)
fid=fopen(FilePath,'r','b');
Version=fread(fid,1,Format);
NDim=fread(fid,1,Format);
SizeDataMat=fread(fid,NDim,Format);

if SizeStatus
	DataMat=[];
	return;
end
if size(SizeDataMat,1)~=1
	SizeDataMat=SizeDataMat';
end
[Data,Count]=fread(fid,Format);
if Count~=prod(SizeDataMat)
	error('Count~=prod(SizeSDataMat)')
end
DataMat=reshape(Data,SizeDataMat);
fclose(fid);	
fprintf(1,'\n')
fprintf('End reading data from file...\n\n');	
fprintf(1,'\n\n')
fprintf(1,'Data size:  '); 
for i=1:NDim-1
	fprintf(1,'%g x ',SizeDataMat(i)); 
end
fprintf(1,'%g \n\n',SizeDataMat(NDim)); 
return;
