IsNomAttribute.m 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. % IsNomAttribute.m
  2. %
  3. % This function checks if an attribute is of nominal type and returns true along
  4. % with nominal and numeric maps. Otherwise it returns false.
  5. %
  6. % input:
  7. % attDatatype - the part that describes the attribute after its name
  8. %
  9. % output:
  10. % isNom - boolean value denoting if nominal
  11. % nominalMap - mapping of nominal values to doubles as in an C++ enumeration
  12. % numericMap - mapping of doubles to nominal values
  13. function [isNom, nominalMap, numericMap] = IsNomAttribute(attDatatype)
  14. openCurl = strfind(attDatatype, '{');
  15. closeCurl = strfind(attDatatype, '}');
  16. if (isempty(openCurl) && isempty(closeCurl))
  17. isNom = false;
  18. nominalMap = containers.Map;
  19. numericMap = containers.Map;
  20. return;
  21. end
  22. assert(length(openCurl) == 1, ['Invalid attribute datatype ' attDatatype]);
  23. assert(length(closeCurl) == 1, ['Invalid attribute datatype ' attDatatype]);
  24. attDatatype = attDatatype(openCurl+1:closeCurl-1);
  25. % remove spaces from nominal
  26. attDatatype = attDatatype(~isspace(attDatatype));
  27. keys = split(attDatatype, ',');
  28. values = 0:length(keys)-1;
  29. nominalMap = containers.Map(keys, values);
  30. % convert to simple when we have single key. Otherwise the type is invalid for map creation
  31. if (length(keys) == 1)
  32. keys = string(keys);
  33. end
  34. numericMap = containers.Map(values, keys);
  35. isNom = true;
  36. end