在具有部分字符串匹配的基础工作空间中查找变量(Matlab) [英] Find variables in base Workspace with partial string match (Matlab)

查看:582
本文介绍了在具有部分字符串匹配的基础工作空间中查找变量(Matlab)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何通过仅输入其名称的一部分在基本MATLAB Workspace中查找变量.我有一长串的变量&我不知道确切的变量名称.是否有一个功能可以比较/匹配变量字符串列表中的字符顺序?

I'd like to know how to find a variable in the base MATLAB Workspace by entering only a part of its name. I have a long list of variables & I don't know the exact variable name. Is there a function that compares/matches character order in a list of variable strings?

谢谢

推荐答案

您可以使用 who 以获得工作空间中当前所有变量名的列表.从那里,您可以使用 regexpi 进行案例不敏感 正则表达式查找来查找与查询匹配的变量.像这样:

You can use who to obtain a list of all variable names currently in your workspace. From there, you can use regexpi to do a case insensitive regular expression lookup to find those variables that match your query. Something like:

namesWorkspace = who;
outStr = regexpi(namesWorkspace, 'nameOfVariable');
ind = ~cellfun('isempty',outStr);
vars = namesWorkspace(ind);

nameOfVariable是要搜索的变量的名称或部分名称. outStr将为您提供元素的单元格数组,该数组的大小与工作空间中的变量总数相同.如果此输出单元格数组中的元素为空,则对应的工作空间变量不匹配.如果它是非空,则表示匹配.我们只需遍历此输出单元格数组并确定哪些位置为非空,然后使用它们索引到工作区名称数组中以检索所需的最终变量(存储在vars中). cellfun 允许您遍历单元格数组中的每个元素,然后对它应用功能.在这种情况下,我们想使用 .因为我们想要相反的,所以我们需要反转运算,因此使用~.

nameOfVariable is the name or partial name of the variable you are searching for. outStr will provide you with a cell array of elements that is the same size as the total number of variables in your workspace. If an element in this output cell array is empty, then the corresponding workspace variable wasn't matched. If it's non-empty, then there was a match. We simply go through this output cell array and determine which locations are non-empty, and we use these to index into our workspace names array to retrieve those final variables you want (stored in vars). cellfun allows you to iterate over every element in a cell array and apply a function to it. In this case, we want to check every cell to see if it's empty by using isempty. Because we want the opposite, we need to invert the operation, and so ~ is used.

例如,这是我最近回答问题后的工作空间:

For example, this is my workspace after recently answering a question:

names = 

    'O'
    'ans'
    'cellData'
    'idx'
    'names'
    'out'
    'possible_names'
    'possible_surnames'
    'student_information'

让我们找到包含单词possible的那些变量名:

Let's find those variable names that contain the word possible:

outStr = regexpi(namesWorkspace, 'possible');
ind = ~cellfun('isempty',outStr);
vars = namesWorkspace(ind)

vars = 

    'possible_names'
    'possible_surnames'

更简单

这顶帽子的提示去给山姆·罗伯茨(Sam Roberts).您可以简单地应用-regexp标志并指定要查找的模式:

Even simpler

Tip of the hat goes to Sam Roberts for this tip. You can simply apply the -regexp flag and specify the patterns you want to look for:

vars = who('-regexp', 'possible')

vars = 

'possible_names'
'possible_surnames'

这篇关于在具有部分字符串匹配的基础工作空间中查找变量(Matlab)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆