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

📄 readbin.m

📁 这是一个关于MATLAB的函数
💻 M
字号:
function resp = readbin(nn,index,timeout)
%READBIN Retrieves a block of DSP memory as binary strings.
%   DN = READBIN(NN)
%   DN = READBIN(NN,[],TIMEOUT)
%   DN = READBIN(NN,INDEX)
%   DN = READBIN(NN,INDEX,TIMEOUT)
%
%   HS = READBIN(NN) - returns a binary string representation of 
%   the DSP's numeric values.  For arrays, the returned values will 
%   be a cell array of binary strings.  Conversely, if MM.SIZE equals 1,
%   (indicating a scalar), the output is an array of hex characters. 
%
%   DN = READBIN(MM,TIMEOUT) - The time alloted to perform the read is 
%   limited  by the MM.TIMEOUT property of the MM object.  However, 
%   this method can be used to explicitly define a different timeout
%   for the read.  For example, this may be necessary for very large 
%   data transfers.
%  
%   See also WRITE, READ, CAST, NUMERIC.

%   Copyright 2002 The MathWorks, Inc.
%   $Revision: 1.6 $ $Date: 2002/05/15 19:31:06 $

error(nargchk(1,3,nargin));
if ~ishandle(nn),
    error('First Parameter must be a NUMERIC Handle.');
end

% Call base class (memoryobj) to get unformatted data
if nargin == 1,
    uidata  = read_memoryobj(nn);
    uidata  = reshape(uidata,nn.storageunitspervalue,[]);
    nvalues = prod(nn.size);
elseif nargin == 3 & isempty(index),
    uidata  = read_memoryobj(nn,[],timeout);
    uidata  = reshape(uidata,nn.storageunitspervalue,[]);
    nvalues = prod(nn.size);
elseif nargin >= 2,
    % Re-arrange data as described by object
    if nargin == 2,     dtimeout = nn.timeout;
    else                dtimeout = timeout;
    end
    try
        index_shaped = round(reshape(index,length(nn.size),[])');
    catch
        error(['Index Array must be an (N x '  num2str(length(nn.size)) ') array of indices' ])
    end
    for subscript = index',
        if any(subscript < 1) | any(subscript' > nn.size),
          error(['INDEX has an entry: [' num2str(subscript') '], which exceeds the defined size of object (NN.SIZE)']);
        end
    end
    resp = [];
    addrange = index2addr(nn,index_shaped);
    uidata   = read_memoryobj(nn,addrange',dtimeout);
    uidata   = reshape(uidata,nn.storageunitspervalue,[]);
    nvalues  = size(uidata);
    nvalues  = nvalues(2);
end

% Endianness swap (if necessary)    
if ~strcmp( nn.endianness,'little') & (nn.storageunitspervalue > 1),
    uidata = flipud(uidata);
end

% Trim pre/post bytes (if necessary)
% For now, limited to increments of bitsperstorageunit
if nn.prepad > 0 |  nn.postpad > 0,
    preaus  = nn.prepad/nn.bitsperstorageunit;
    postaus = nn.postpad/nn.bitsperstorageunit;
    if preaus + postaus > nn.storageunitspervalue,
        error(' Pre/Post padding exceeds available memory area');
    end    
    uidata = uidata(1+prepaus:end-postaus,:);
end

% Convert adjusted array of 'valid' au into numeric values (unsigned
% integers)
validaus = size(uidata);  % Not storageunitspervalue (padding!!)
validaus = validaus(1);
scale    = 2.^(0:nn.bitsperstorageunit:nn.bitsperstorageunit*validaus-1)';
for iv=1:nvalues,
   uival = double(uidata(:,iv))';
   fdata(iv) = uival*scale;
end

% Convert adjusted array of 'valid' au into binary array
% Indexed and regular
if length(nn.size) > 1 & ((nargin == 1) | (nargin == 3 & isempty(index))),  % Non-indexed, arrange according to 'arrayorder' prop
%     if strcmp(nn.arrayorder,'row-major'),
%         fdata = fdata( GetMatlabIndex(nn) );
%     end
%     resp = reshape(fdata,nn.size); 
    
else   % Indexed - Don't bother shaping it
    % resp{1} = reshape(fliplr(dec2bin(double(uidata),nn.bitsperstorageunit)'),1,[]);
    resp{1} = dec2bin(fdata,nn.wordsize);
end

if nvalues == 1,
    % resp{1} = reshape(fliplr(dec2bin(double(uidata),nn.bitsperstorageunit)'),1,[]);
    resp{1} = dec2bin(fdata,nn.wordsize);
else % create cell array
	for iv=1:nvalues,
		resp{iv} = reshape(fliplr(dec2bin(double(uidata(:,iv)),nn.bitsperstorageunit)'),1,[]);
	end
	if nargin==1 & length(nn.size) > 1, % reshape only if all values are asked; if indexed, present output in series
        resp = reshape(resp,nn.size);
	end 
end

% [EOF] readbin.m

⌨️ 快捷键说明

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