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

📄 write_rnumerichex.m

📁 这是一个关于MATLAB的函数
💻 M
字号:
function dummy = write_rnumerichex(nn,index,data)
%   Copyright 2002 The MathWorks, Inc.
%   $Revision: 1.4 $  $Date: 2002/05/14 19:47:06 $

error(nargchk(2,3,nargin));
if ~ishandle(nn),
    error('First Parameter must be a RNUMERIC Handle.');
end
if nargin==2,   
    data = index;
end

% Is data a valid input
if ~(iscellstr(data) | ischar(data)),
    error('DATA must be a hexadecimal string (cell array) ');
end

if ischar(data),  % change to 1 character array
    data = cellstr(data);
end

% Reshape value according to ARRAYORDER property
if strcmp(nn.arrayorder,'row-major'),
    data = reshape(permute(data,[2 1]),1,[]);
else  
    data = reshape(data,1,[]);
end

% Do we have sufficent values? 
nvalues = numel(data);
nvaldef = prod(nn.size);
if nvalues < nvaldef,
    warning('DATA has less elements than specified numeric array, DATA will only be applied to beginning of memory area');
elseif nvalues > nvaldef,
    warning('DATA has more elements than specified numeric array, DATA will be be limited to defined memory area');
    data = data{1:nvaldef};  % Truncate!
end


% Convert numeric values into array of AUs 
%  where each column is the the AUs necessary for one numeric value
%  The data will be little-endian (swapped later if necessary)
%  thus cc.write([1 256]), where storageunitspervalue = 3, represent = 'unsigned', bitsperstorageunit = 8
%  fdata = 
%    1  0
%    0  1
%    0  0

len = 1; % length(data); % num of characters
zerostr = '0';  % ugly code but faster than creating string of zeros on the fly 
uidata = [];
for i=1:len, % LEN sHould always be 1
    mustlen = round( nn.bitsperstorageunit * nn.storageunitspervalue/4 ); % total bits in a value div (4 bits in a character)
    needzerochars = mustlen-length(data{i});
    if needzerochars > -1
        data{i}  = [zerostr(ones(1,needzerochars)) data{i}];
    else
        warning(['Overflow: Saturation was required to fit into the specified numeric range']);
        data{i}  = data{i}(1+abs(needzerochars):end);
    end
    newdata  = hex2dec( flipud(reshape(data{i},mustlen/nn.storageunitspervalue,[])') );
    uidata = horzcat(uidata, newdata);
end

% Endianness swap (if necessary), Little endian only
if strcmp( nn.endianness,'big') & (nn.storageunitspervalue > 1),
    uidata = flipud(uidata);
end    

if nn.bitsperstorageunit <= 8, % (we've already applied rounding)
    uidata = uint8(uidata);
elseif nn.bitsperstorageunit <= 16,
    uidata = uint16(uidata);
elseif nn.bitsperstorageunit <= 32,
    uidata = uint32(uidata);
else
    warning('Host computer does not natively support integer values > 32, data truncation likely')
    uidata = uint32(uidata);
end


% if storageunitspervalue > 1
if nn.storageunitspervalue > 1
    uidata = reshape(uidata,[],nn.numberofstorageunits)';
end    

% Write raw values to memory
if nargin == 2,
    for i=1:nn.numberofstorageunits
        regwrite(nn.link,nn.regname{i},uidata(i),'binary',nn.timeout);
    end
elseif nargin == 3, % timeout% index version - one value only!
    for i=0:nn.storageunitspervalue-1
        regwrite(nn.link,nn.regname{index+i},uidata(index+i),'binary',nn.timeout);
    end
elseif nargin == 4 & isempty(index), % timeout specified
    for i=1:nn.numberofstorageunits
        regwrite(nn.link,nn.regname{i},uidata(i),'binary',timeout);
    end
else % timeout specified, non-empty index
    for i=0:nn.storageunitspervalue-1
        regwrite(nn.link,nn.regname{index+i},uidata(index+i),'binary',timeout);
    end
end

% [EOF] write_rnumerichex.m

⌨️ 快捷键说明

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