过滤Matlab表格中的单词(如在Excel中)

时间:2015-02-11 20:31:45

标签: matlab matlab-table

在Excel中,您可以使用“过滤器”功能查找列中的某些单词。我想在整个表格的Matlab中做到这一点。

使用Matlab示例表“patients.dat”作为示例;我的第一个想法是使用:

patients.Gender=={'Female'}

哪个不起作用。

strcmp(patients.Gender,{'Female'})

仅在一列(“性别”)中工作。

我的问题:我的桌子上有不同的单词,上面写着'A','B','香蕉','苹果',......在桌子的各栏中以任意方式展开。我只想要包含“A”和“B”的行。

奇怪的是我没有在matlab“帮助”中找到这个,因为它看起来很基本。我看着stackO,但也没有找到答案。

2 个答案:

答案 0 :(得分:1)

Matlab中的table可以看作是扩展cell array。例如,它还允许命名列。

但是,在您的情况下,您希望搜索整个cell array,而不关心table的任何额外功能。因此,请将其转换为table2cell

然后你想搜索某些单词。您可以使用regexp,但在您提及strcmp的示例中也足够了。两者都可以立即在cell arrays上工作。

最后,您只需要find逻辑搜索矩阵的行。

这里是一个示例,它可以从Matlab示例数据集中获取所有“男性”和“优秀”条件患者的行:

patients = readtable('patients.dat');
patients_as_cellarray = table2cell(patients);
rows_male = any(strcmp(patients_as_cellarray, 'Male'), 2); % is 'Male' on any column for a specific row
rows_excellent = any(strcmp(patients_as_cellarray, 'Excellent'), 2); % is 'Excellent' on any column for a specific row
rows = rows_male & rows_excellent; % logical combination
patients(rows, :)
确实只打印出状况良好的男性患者。

答案 1 :(得分:1)

以下是一种更简单,更优雅的语法:

<?php
// start session
session_start();
// check if username is admin

replace it with your admin user name

$adminUsername = "admin";
if($_SESSION['login_user'] !== $adminUsername){
    // isn't admin, redirect them to a different page
    header("Location: /someotherpage.php");
}

?>

请注意,在此示例中,您需要确保patient.Gender是一种分类类型。您可以使用 @Repository public interface AlbumRepository extends CrudRepository<Album, Integer> { public List<Album> findByArtists_Id(Integer artists_id) ; } 将变量转换为分类变量,并将其重新分配给表变量,如下所示:

matches = ((patients.Gender =='Female') & (patients.Age > 26));
subtable_of_matches = patients(matches,:);

% alternatively, you can select only the columns you want to appear,
% and their order, in the new subtable.
subtable_of_matches = patients(matches,{'Name','Age','Special_Data'});

以下是您的参考:https://www.mathworks.com/matlabcentral/answers/339274-how-to-filter-data-from-table-using-multiple-strings