AppendArff.m 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. % function AppendArff:
  2. %
  3. % Loads the given ARFF file appends the provided attribute and saves the data
  4. % the original file
  5. %
  6. % input:
  7. % arffFile - file to load the data from
  8. % attData - attribute data
  9. % attName - attribute name
  10. % attType - attribute type (Integer or Numeric accepted for now)
  11. % extraId - extension to add to the name before file extension
  12. function AppendArff(arffFile, attData, attName, attType, extraId)
  13. if (nargin<5)
  14. extraId = '';
  15. end
  16. % initially load data
  17. [data, metadata, attributes, relation] = LoadArff(arffFile);
  18. % check if attribute already exists
  19. for i=1:size(attributes,1)
  20. if (strcmpi(attributes{i,1}, attName))
  21. error(['Attributes "' attName '" already exists in file ' arffFile]);
  22. end
  23. end
  24. % check for data and attribute values
  25. assert(size(data,1)==size(attData,1), 'Attribute data and arff data should be of the same length');
  26. % set data
  27. appData = zeros(size(data,1), size(data,2)+1);
  28. appData(:,1:end-1) = data;
  29. appData(:,end) = attData;
  30. % append attribute
  31. index = size(attributes,1)+1;
  32. attributes{index,1} = attName;
  33. attributes{index,2} = attType;
  34. [dir, name, ext] = fileparts(arffFile);
  35. outArffFile = [name extraId ext];
  36. if (~isempty(dir))
  37. outArffFile = [dir '/' outArffFile];
  38. end
  39. % save to the same file
  40. SaveArff(outArffFile, appData, metadata, attributes, relation);
  41. end