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

📄 fileswapendian.m

📁 toolbox of BVQX, This is the access between BV and matlab. It will help you to analysis data from BV
💻 M
字号:
function fileswapendian(filename, wordsize, iooff, iosize)
% fileswapendian  - swap endian type of (partial) file content
%
% FORMAT:       fileswapendian(filename, wordsize [, iooff [, iosize]])
%
% Input fields:
%
%       filename    filename
%       wordsize    endian wordsize, either of [2, 4, 8, 16]
%       iooff       1x1 value, default 0
%       iosize      1x1 value, default til end of file

% Version:  v0.7a
% Build:    7080809
% Date:     Aug-08 2007, 9:17 AM CEST
% Author:   Jochen Weber, Brain Innovation, B.V., Maastricht, NL
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% argument check
if nargin < 2 || ...
   ~ischar(filename) || ...
    isempty(filename) || ...
    exist(filename(:)', 'file') ~= 2 || ...
   ~isa(wordsize, 'double') || ...
    numel(wordsize) ~= 1 || ...
   ~any([2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64] == wordsize)
    error( ...
        'BVQXtools:BadArgument', ...
        'Bad or missing argument.' ...
    );
end
filename = filename(:)';
filedir = dir(filename);
filesiz = filedir.bytes;
if nargin < 3
    iooff = 0;
end
if nargin < 4
    iosize = Inf;
end
if ~isa(iooff, 'double') || ...
    numel(iooff) ~= 1 || ...
    isinf(iooff) || ...
    isnan(iooff) || ...
    iooff < 0 || ...
    iooff ~= fix(iooff) || ...
   ~isa(iosize, 'double') || ...
    numel(iosize) ~= 1 || ...
    isnan(iosize) || ...
    iosize < wordsize || ...
    (iosize / wordsize) ~= fix(iosize / wordsize) || ...
    (~isinf(iosize) && ...
     (iooff + iosize) > filesiz)
    error( ...
        'BVQXtools:BadArgument', ...
        'Invalid argument combination.' ...
    );
end
if isinf(iosize)
    iosize = filesiz - iooff;
end
ioend = iooff + iosize;

% open file for read/write access
try
    fid = fopen(filename, 'r+', 'ieee-le');
    if fid < 1
        error('BAD_FILE_ID');
    end
    fseek(fid, iooff, -1);
    if ftell(fid) ~= iooff
        error('SEEK_FAILED');
    end
catch
    error( ...
        'BVQXtools:FileReadError', ...
        'Error opening/seeking file.' ...
    );
end

% create a loop to allow working on large files
lrgread = 3145728 / wordsize;
for cpos = iooff:3145728:ioend
    
    % seek to position
    fseek(fid, cpos, -1);
    
    % for larger chunks
    if (cpos + 3145728) <= ioend
        
        % read content
        bytecont = fread(fid, [wordsize, lrgread], '*uint8');
        
        % seek back
        fseek(fid, cpos, -1);
        
        % write with order swapped
        fwrite(fid, bytecont(end:-1:1, :), 'uint8');
        
    % for smaller chunks
    else
        
        % calc number of elements
        smlread = (ioend - cpos) / wordsize;
        
        % check number
        if smlread ~= fix(smlread)
            fclose(fid);
            error( ...
                'BVQXtools:InternalError', ...
                'Error accessing file.' ...
            );
        end
        
        % then read/seek/write
        bytecont = fread(fid, [wordsize, smlread], '*uint8');
        fseek(fid, cpos, -1);
        fwrite(fid, bytecont(end:-1:1, :), 'uint8');
    end
end

% close file
fclose(fid);

⌨️ 快捷键说明

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