⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fs_read_surf.m

📁 Matlab下的EEG处理程序库
💻 M
字号:
function [vertex_coords, faces] = fs_read_surf(fname)

% FS_READ_SURF - FREESURFER I/O function to read a surface file
% 
% [vertex_coords, faces] = fs_read_surf(fname)
% 
% Reads the vertex coordinates (mm) and face lists from a surface file.
% 
% Note that reading the faces from a quad file can take a very long
% time due to the goofy format that they are stored in. If the faces
% output variable is not specified, they will not be read so it 
% should execute quicker.
% 
% Try this to visualize the surface:
% patch('vertices',vertex_coords,'faces',faces,...
%       'facecolor',[.5 .5 .5],'edgecolor','none')
% camlight('headlight','infinite')
% 
% See also FS_WRITE_SURF, FS_READ_CURV, FS_READ_WFILE

%fid = fopen(fname, 'r') ;
%nvertices = fscanf(fid, '%d', 1);
%all = fscanf(fid, '%d %f %f %f %f\n', [5, nvertices]) ;
%curv = all(5, :)' ;



%QUAD_FILE_MAGIC_NUMBER =  (-1 & 0x00ffffff) ;
%NEW_QUAD_FILE_MAGIC_NUMBER =  (-3 & 0x00ffffff) ;

TRIANGLE_FILE_MAGIC_NUMBER  =  16777214 ;
QUAD_FILE_MAGIC_NUMBER      =  16777215 ;


% open it as a big-endian file
fid = fopen(fname, 'rb', 'b') ;
if (fid < 0),
    str = sprintf('could not open surface file %s.', fname) ;
    error(str) ;
end

magic = fs_fread3(fid) ;

if (magic == QUAD_FILE_MAGIC_NUMBER),
    Nvertices = fs_fread3(fid) ;
    Nfaces = fs_fread3(fid) ;
    fprintf('...reading %d quad file vertices',Nvertices); tic;
    vertex_coords = fread(fid, Nvertices*3, 'int16') ./ 100 ; 
    t=toc; fprintf('...done (%6.2f sec)\n',t);
    if (nargout > 1),
        fprintf('...reading %d quad file faces (please wait)\n',Nfaces); tic;
        faces = zeros(Nfaces,4);
        for iface = 1:Nfaces,
            for n=1:4,
                faces(iface,n) = fs_fread3(fid) ;
            end
            if(~rem(iface, 10000)), fprintf(' %7.0f',iface); end
            if(~rem(iface,100000)), fprintf('\n'); end
        end
        t=toc; fprintf('\n...done (%6.2f sec)\n',t);
    end
elseif (magic == TRIANGLE_FILE_MAGIC_NUMBER),
    fprintf('...reading triangle file...'); tic;
    tline = fgets(fid); % read creation date text line
    tline = fgets(fid); % read info text line
    
    Nvertices = fread(fid, 1, 'int32') ; % number of vertices
    Nfaces = fread(fid, 1, 'int32') ; % number of faces
    
    % vertices are read in column format and reshaped below
    vertex_coords = fread(fid, Nvertices*3, 'float32');
    
    % faces are read in column format and reshaped
    faces = fread(fid, Nfaces*3, 'int32') ;
    faces = reshape(faces, 3, Nfaces)' ;
    t=toc; fprintf('done (%6.2f sec)\n',t);
else
    str = sprintf('unknown magic number in surface file %s.', fname) ;
    error(str) ;
end

vertex_coords = reshape(vertex_coords, 3, Nvertices)' ;
fclose(fid) ;

fprintf('...adding 1 to face indices for matlab compatibility.\n');
faces = faces + 1;

return

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -