Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

% Function Name: sortbyHeader % Inputs (1): - (char) The file name of an Excel f

ID: 3537667 • Letter: #

Question

% Function Name: sortbyHeader

% Inputs (1): - (char) The file name of an Excel file

%              - (char) Name of a potential column header

% Outputs (1): - (cell) Cell array containing the sorted excel data *OR*

%                       (char) a string stating that the row header does

%                       not exist.

%

% Function Description:  

%            Given the file name of an Excel file and the name of a potential

%            column header on the first row, write a function, sortbyHeader, that

%            will sort the excel array based on the data under a specific column

%            header. This function works in a similar way that the sort function

%            works in Excel. If the data in the column of interest are strings, then

%            the function will sort all the rows in such a way that the information

%   under the column of interest are sorted alphabetically. Likewise, if

%            the data under the desired column header are numbers, then the function

%            will sort all the rows so that the data in the column of interest are

%            in ascending numerical order. If the column header in the second input

%            does not exist, then return the following string: 'Invalid column

%            header'.

%

% Note:

%   - When sorting, in the event that there is duplicate information, (i.e.

%     multiple instances of the same number or the same word in the

%     column), list row data in the order that they appear in the Excel

%     file from top to bottom. For example, if 'Clemson' appears on the

%     3rd and 14th row, the output will feature row data from the 3rd row,

%     then row data from the 14th row. The sort() function will account

%     for this condition automatically.

%   - You may assume that the data under any of the column headers will be

%     either characters or numbers

%   - Sort characters based on their ASCII values. Thus, for example, 'Z'

%     would come before 'a' if the two letters were to be sorted.

%   - The headers *are* case sensitive.

%

% Test Cases:

%            x = 'techScores.xls';

%

%   scores1 = sortbyHeader(x, 'Opponent');

%   scores1 -> 'Opponent'              'Tech Points'    'Opponent Points'

%               'Clemson'               [         30]    [             27]

%               'Clemson'               [         39]    [             34]

%               'Duke'                  [         49]    [             10]

%              'Florida State'         [         49]    [             44]

%               'Georgia'               [         24]    [             30]

%               'Iowa'                  [         14]    [             24]

%               'Jacksonville State'    [         37]    [             17]

%               'Miami'                 [         17]    [             33]

%               'Mississippi State'     [         42]    [             31]

%               'North Carolina'        [         24]    [              7]

%               'Vanderbilt'            [         56]    [             31]

%               'Virginia'              [         34]    [              9]

%               'Virginia Tech'         [         28]    [             23]

%               'Wake Forest'           [         30]    [             27]

%

% scores2 = sortbyHeader(x, 'Tech Points');

% scores2 ->   'Opponent'              'Tech Points'   'Opponent Points'

%               'Iowa'                  [         14]   [             24]

%               'Miami'                 [         17]   [             33]

%               'North Carolina'        [         24]   [              7]

%               'Georgia'               [         24]   [             30]

%               'Virginia Tech'         [         28]   [             23]

%               'Clemson'               [         30]   [             27]

%               'Wake Forest'           [         30]   [             27]

%               'Virginia'              [         34]   [              9]

%               'Jacksonville State'    [         37]   [             17]

%               'Clemson'               [         39]   [             34]

%               'Mississippi State'     [         42]   [             31]

%               'Florida State'         [         49]   [             44]

%               'Duke'                  [         49]   [             10]

%               'Vanderbilt'            [         56]   [             31]

%

% scores3 = sortbyHeader(x, 'teCH PoINts');

% scores3 -> 'Invalid column header'

techPoints kls file from test case

Explanation / Answer

% THIS WAS a really difficult one..you whould try to understand the code thoroughly

function out = sortbyHeader(file,col)

[ndata, text, alldata] = xlsread(file);

[r , c ] =size(alldata);

alldata2=alldata;

reqd =0 ;

converts =zeros(1,c);

for i=1:c

if(strcmp(alldata{1,i}, col))

reqd=i;

end

if(isnumeric(alldata{2,i}))

alldata(2:r,i)=cellstr(num2str(cell2mat(alldata(2:r,i))));

converts(i)=1;

end

end

if reqd == 0

out= 'Invalid column header';

else

alldata(2:r,:)=sortrows(alldata(2:r,:),reqd);

for i=1:c

if( converts(i) == 1)

alldata(2:r,i)=num2cell(str2num(cell2mat(alldata(2:r,i))));

end

end

out=alldata;

end

end