AddAttArff.m 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. % function AddAttArff:
  2. %
  3. % This function adds the data and the name of the new attribute to the initial
  4. % data as a new column.
  5. %
  6. % input:
  7. % data - data of the initial arff file
  8. % attributes - attributes of the initial arff file
  9. % attData - attribute data to append at the data. When nominal attributes
  10. % are appended the attribute values should corespond to the enumeration
  11. % equivalent
  12. % attName - attribute name
  13. % attType - attribute type (Integer, Numeric or nominal in the form '{val1,val2}')
  14. %
  15. % output:
  16. % newData - data after addition of the new column
  17. % newAttributes - attributes containing the addition of the new attribute
  18. function [newData, newAttributes] = AddAttArff(data, attributes, attData, attName, attType)
  19. % are data and new attribute smae size
  20. assert(size(data,1)==size(attData,1), 'Provided attribute does not have same number of entries as initial data');
  21. % check if attribute already exists
  22. for i=1:size(attributes,1)
  23. if (strcmpi(attributes{i,1}, attName))
  24. error(['Attributes "' attName '" already exists. Cannot add it.']);
  25. end
  26. end
  27. % merge returned attributes
  28. newAttributes = attributes;
  29. index = size(attributes,1)+1;
  30. newAttributes{index,1} = attName;
  31. newAttributes{index,2} = attType;
  32. % concatenate attribute to the returned data
  33. newData = zeros(size(data,1), size(data,2)+1);
  34. newData(:,1:end-1) = data(:,:);
  35. newData(:,end) = attData(:);
  36. end