【发布时间】:2014-10-15 00:39:26
【问题描述】:
好的,所以我觉得这应该是一个非常简单的问题,但我已经在努力解决它了。这很糟糕,因为我下周要参加考试,而我几乎不了解我们需要知道的一半内容。然而,这不是重点。我需要做的是编写一个函数,该函数接收一个 Excel 文件和一个 Header(这将是我们输入的字符串)。然后它在文件中找到标题并对其进行排序(如果列由字符组成,则按字母顺序排列,如果有数字则按升序排列)。
输入:
测试用例:
scores = sortByHeader(x, 'Opponent');
scores => '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]
我很难弄清楚如何识别问题中的标题。到目前为止我有:
function[scores] = sortByHeader(File, Name)
[num, txt, raw] = xlsread(File); %// Reads in the file
%// Gives me the dimensions of the file
[r,c] = size(raw);
%// I want to look through all of the columns until I find the header I need
for i = 1:c
%// I'm attempting to search through the file here
if strcmp(raw(1, i), Name)
%// Here's my issue. When it finds the name, I am not sure what to do with it then
Name_Column = 1;
end
%// I tried to mask it, but this doesn't actually work.
raw(Name_Column) = raw;
%// How I plan to sort everything once I find it. Though I believe I need to adjust this slightly to solely account for the 'Name' Column.
scores = sort(raw, 'ascend');
此时我最需要提示。我可能应该自己解决这些问题,但这说起来容易做起来难。注意:标题并不总是在同一个地方,可以有任意数量的行或列。
【问题讨论】:
标签: matlab sorting if-statement io data-extraction