sortNEV.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. function [SortedStruct, SortedIndeces] = sortNEV(STRUCT, field, TestValue, Report)
  2. % This function extracts information out of a structure based on the field
  3. % values. The outputs can be the sorted structure and the indeces of those
  4. % extracted fields.
  5. %
  6. % Use [SortedStruct, SortedIndeces] = sortNEV(STRUCT, field, TestValue, Report)
  7. %
  8. % STRUCT: The structure to be used for extraction.
  9. %
  10. % field: The field in STRUCT that is set as criteria for extraction.
  11. % This parameter is optional. If not passed, the function
  12. % will prompt the user to provide the name of the field.
  13. %
  14. % TestValue: The value that "field" needs to be equal to in order to
  15. % get extracted.
  16. % This parameter is optional. If not passed, the function
  17. % will prompt the user to provide the value.
  18. %
  19. % Report: If this flag is set to 1 the function will show a short
  20. % summary of the data that was processed.
  21. % This parametere is optional.
  22. % DEFAULT: will not show report.
  23. %
  24. % OUTPUT
  25. %
  26. % SortedStruct: The sorted structure output.
  27. %
  28. % SortedIndeces: The indeces to the sorted elements.
  29. %
  30. % Example:
  31. %
  32. % [NewStruct, Indeces] = sortNEV(MainStruct, 'TestField', 3, 1);
  33. %
  34. % In the example above, the MainStruct is the structure containing all
  35. % elements. The function will search through MainStruct and extract all
  36. % elements that have their 'TestField' field set to '3'. It will also
  37. % show a report of how many elemenets were processed and how many were
  38. % extracted.
  39. %
  40. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  41. % Kian Torab
  42. % ktorab@blackrockmicro.com
  43. % Blackrock Microsystems
  44. %
  45. % Version 1.4.1.0
  46. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  47. %% Validates all passed on parameters and will prompt user for any missing
  48. % parameter.
  49. if ~exist('field', 'var')
  50. field = input('What is the name of the field of interest? ');
  51. end
  52. while ~isfield(STRUCT, field)
  53. display('The field name does not exist. Try again.');
  54. field = input('What is the name of the field of interest? ');
  55. end
  56. if ~exist('TestValue', 'var')
  57. TestValue = input('What value does the field need to be equal to (string format)? ');
  58. end
  59. if ~exist('Report', 'var')
  60. Report = 0;
  61. end
  62. %% Searches through the structure to extract the elements of interest.
  63. SortedIndeces = NaN(1,length(STRUCT));
  64. if isa(TestValue, 'double')
  65. % display(['Please verify to make sure the field ' field ' does not contain any [] data or the sort will not be successful.'])
  66. for strIDX = 1:length(STRUCT)
  67. if find(STRUCT(strIDX).(field) == TestValue)
  68. SortedIndeces(strIDX) = 1;
  69. else
  70. SortedIndeces(strIDX) = 0;
  71. end
  72. end
  73. SortedStruct = STRUCT(find(SortedIndeces==1));
  74. else
  75. for strIDX = 1:length(STRUCT)
  76. if strcmpi(STRUCT(strIDX).(field), TestValue)
  77. SortedIndeces(strIDX) = 1;
  78. else
  79. SortedIndeces(strIDX) = 0;
  80. end
  81. end
  82. SortedStruct = STRUCT(find(SortedIndeces==1));
  83. end
  84. %% Will define as an empty structure of no instances were found.
  85. if ~exist('SortedStruct', 'var')
  86. display('No instances found.');
  87. SortedStruct = struct([]);
  88. else
  89. %% If the flag is set, it will show a report of how many files were
  90. % processed.
  91. if Report == 1
  92. if isempty(SortedStruct)
  93. display('No instances found.');
  94. else
  95. display([num2str(length(SortedStruct)) ' many instances were found and extracted.']);
  96. end
  97. end
  98. %% Will force the function to send an output argument to the caller
  99. % environment.
  100. if (nargout == 0)
  101. assignin('caller', 'SortedStruct', SortedStruct);
  102. end
  103. end