📄 adddatafield.m.svn-base
字号:
function d = addDataField(d, name, value, type, valueName)
%Add a field to a d struct
%This copies data and so needs extra memory, unless you specify valueName,
%in which case the old value dissapears!
% Copyright (C) 2006 Charanpal Dhanjal
% This library is free software; you can redistribute it and/or
% modify it under the terms of the GNU Lesser General Public
% License as published by the Free Software Foundation; either
% version 2.1 of the License, or (at your option) any later version.
%
% This library is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
% Lesser General Public License for more details.
%
% You should have received a copy of the GNU Lesser General Public
% License along with this library; if not, write to the Free Software
% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
% USA
if (nargin < 4)
fprintf('%s\n', help(sprintf('%s', mfilename)));
error('Incorrect number of inputs - see above usage instructions.');
elseif nargin == 4
valueName = '';
end
if ~strcmp(type, 'examples') & ~strcmp(type, 'labels') & ~strcmp(type, 'kernel') & ~strcmp(type, 'indices')
error('Invalid data field type')
end
numExamples = size(value, 1);
numCols = size(value, 2);
if d.numExamples~= 0 & size(value, 1) ~= d.numExamples
error('Added field does not contain correct number of examples');
end
if isfield(d.exampleIndices, name)
error('Field %s already exists', name);
end
if d.numExamples == 0
d.numExamples = size(value, 1);
end
global dataValues;
global dataValuesRefCount;
global dataValuesSlots;
if isempty(dataValues) & isempty(dataValuesRefCount) & isempty(dataValuesSlots)
dataValues = cell(1, 1);
dataValuesRefCount = 1;
dataValuesSlots = 0;
end
nextIndex = min(find(dataValuesSlots == 0));
%Bit of a messy way to save memory, but will do
if strcmp(valueName, '');
dataValues{nextIndex, 1} = value;
else
try
evalin('caller', [sprintf('global dataValues; dataValues{%d, 1} = ', nextIndex) , valueName, '; clear ', valueName]);
catch
lerror = lasterror;
error('Variable Rename Failed: %s.', lerror.message)
end
end
dataValuesRefCount(nextIndex, 1) = 1;
dataValuesSlots(nextIndex, 1) = 1;
d.globalIndices.(name) = nextIndex;
d.colIndices.(name) = (1:numCols)';
d.exampleIndices.(name) = (1:d.numExamples)'; %Internal indexing
d.types.(name) = type;
%Add an item to the slots if not there
if sum(dataValuesSlots == 0) == 0
dataValuesSlots = [dataValuesSlots; 0];
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -