function [selectedStudents] = selectstudents(totalStudents,varargin) %selectstudents will randomly select students from a pre-defined list of %students in the class. selectstudents requires one input argument: the %number of students in the class and two optional input arguments: the %number of students to select at random and a list of student ID numbers to %exclude from the selection. % %selectstudents(28) will randomly select 1 student from a class of 28 students %selectstudents(28,3) will randomly select 3 students %selectstudents(28,3,[1 2 3]) will randomly select 3 students from the class %and exclude the students with IDs 1,2, and 3 % %The output of selectstudents will be an array of students and there ID %numbers. A row in the array will contain the ID number of the student in %the first column and the name of the student in the second column %Daniel Brateris %Define the list of students students = {'Kevin_A' 'Kenneth' 'Daniel' 'Timothy' 'Phillip' 'Adolfo' 'Jessica_M' 'Colin' 'Jessica_T' 'Trevor' 'Andrew' 'Brian' 'Michael_A' 'Allan' 'Brendan_R' 'Brendan_J' 'Richard_J' 'Wendell' 'David_M' 'Michael_S' 'Mohak' 'Kevin R.' 'Robert_W' 'Matthew' 'Richard_A' 'Eric' 'Robert_F' 'Christopher'}; %Check to make sure totalStudents is <= the number of students in the array students if(totalStudents > size(students,2)) errordlg('The number oif students must be less than 29'); return end %Check for additional arguments and assign them to variables %Check for number of students to select if(size(varargin,2) >= 1) num2select = varargin{1}; else num2select = 1; end %Check for excluded students if(size(varargin,2) >= 2) excludedIDs = varargin{2}; else excludedIDs = 0; end %Create an array for the selected students selectedStudents = zeros(0,2); %Create an array to hold previously choosen random numbers %used to ensure that a double selection does not occur previousRandomNumbers = []; %Reset the random number generator rand('twister',sum(100*clock)) i = 0; while (i < num2select) %numberOK used to track if the random generated number was %previously generated by the randint function, initially assume OK numberOK = true; %Generate a random integer between 1 and total_students randomNumber = randint(1,1,[1,totalStudents]); %Check if the random number is one of the excluded ID's %if so, this is not a valid random number, set numberOK to false for j=1:length(excludedIDs) if (randomNumber == excludedIDs(j)) numberOK = false; end end %Check if the random number has already been generated by randint %if so this in not a valid random number, set numberOK to false for j=1:length(previousRandomNumbers) if (randomNumber == previousRandomNumbers(j)) numberOK = false; end end %Check if the random number is OK, if so add the corrosponding %student to the selected students list and increment the counter i %if the random number is OK add it to previousRandomNumbers if (numberOK) previousRandomNumbers = [previousRandomNumbers randomNumber]; selectedStudents = [selectedStudents; randomNumber students(randomNumber)]; i = i + 1; end end