【问题标题】:Sorting By Excel Headers in MATLAB (High Level I/O)在 MATLAB 中按 Excel 标题排序(高级 I/O)
【发布时间】: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


    【解决方案1】:

    如您在上面定义的那样,标题将始终位于变量“raw”的第一行。

    headerMatch = strcmp(raw, Name);
    whichColumn = find(headerMatch(1,:));
    

    这将返回一个逻辑数组 headerMatch,在与您的字符串 Name 匹配的位置为 1,并且 whichColumn 将返回列号,因为我们只查看第一行。

    然后就是拉出您使用这些命令识别的列并对其进行排序,这是您之前苦苦挣扎的地方。有几种方法可以处理元胞数组,它们看起来可能不一致,但归结为您想要的输出。如果您想要一个作为当前数组子部分的元胞数组,请使用括号,就像您用于矩阵寻址一样。如果您试图获取要处理的单元格内的值,请使用大括号。在下面的两个排序调用中查看如何提取字符串元胞数组或创建向量的值的不同之处。对于字符串元胞数组和数字向量的排序命令略有不同,这就是为什么有两个不同的调用具有两种不同的参数格式。只需先检查您要处理的是哪一个,然后传递给适当的排序函数。

    if ischar(raw{2, whichColumn}) % ischar checks if the first cell we want to sort has numbers or letters in it
    
        sortedColumn = sort(raw(2:end, whichColumn)); % Sort a cell array of strings
    
    else 
    
        sortedColumn = sort(vertcat(raw{2:end, whichColumn}), 'ascend'); % Sort a vector of numbers
    
    end
    

    编辑:要返回按标题名称指定的给定列排序的整个电子表格数据(不包括标题),请提取上述列索引(whichColumn 变量)。然后使用“sortrows”命令按该列对电子表格数据的元胞数组进行排序。

    sortedMatrix = sortrows(raw(2:end, :), whichColumn);
    

    【讨论】:

    • 我认为这并不难。我如何将该 sortedColumn 索引到初始列中?我试图将其编入索引,但没有奏效。我要试戴口罩,看看有没有帮助。顺便谢谢你:)
    • 您的意思是返回已排序的指定列的导入矩阵?
    • 是的。我需要这些行与正确的变量匹配。您可以将最终输出视为我的测试用例。我想我可能需要将它从细胞类型或其他东西中转换出来。我在玩它。
    • 哦,这比我做的还要容易。你会想要'sortrows'命令。给它提供去掉标题的数据以及排序依据的列,它会处理其余的。请参阅上面的编辑。
    • 我只需要 MATLAB 拥有的所有命令的主列表。这将使生活轻松十倍。
    猜你喜欢
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 2018-01-25
    • 2011-06-20
    • 2013-12-25
    • 1970-01-01
    • 2014-01-05
    • 1970-01-01
    相关资源
    最近更新 更多