通过检查前几个字符来识别相关的字符串信息 [英] Recognize relevant string information by checking the first characters

查看:90
本文介绍了通过检查前几个字符来识别相关的字符串信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含2列的表格.在第一列中,我有一个字符串信息,在第二列中,我有一个逻辑索引

I have a table with 2 columns. In column 1, I have a string information, in column 2, I have a logical index

%% Tables and their use

T={'A2P3';'A2P3';'A2P3';'A2P3 with (extra1)';'A2P3 with (extra1) and (extra 2)';'A2P3 with (extra1)';'B2P3';'B2P3';'B2P3';'B2P3 with (extra 1)';'A2P3'};
a={1 1 0 1 1 0 1 1 0 1 1 }

T(:,2)=num2cell(1);
T(3,2)=num2cell(0);
T(6,2)=num2cell(0);
T(9,2)=num2cell(0);

T=table(T(:,1),T(:,2));

class(T.Var1);
class(T.Var2);

T.Var1=categorical(T.Var1)
T.Var2=cell2mat(T.Var2)

class(T.Var1);
class(T.Var2);

if T.Var1=='A2P3' & T.Var2==1
    disp 'go on'
else
    disp 'change something'
end

更新:

  • 一旦我知道如何将工作空间复制为代码格式,我将立即更新本节

**仍然不知道该怎么做,但是就可以了

** still don't know how to do that but here it goes

***为什么使用表是一把双刃剑(但仍然很酷):我必须非常清楚表内部的类,以便在if else构造中引用它,这里我不得不转换两列进行分类并从单元格中翻倍以使其起作用...

*** why working with tables is a double edged sword (but still cool): I have to be very aware of the class inside the table to refer to it in an if else construct, here I had to convert two columns to categorical and to double from cell to make it work...

这是我的数据:

我想要这个:

if T.Var1=='A2P3*************************' & T.Var2==1
    disp 'go on'
else
    disp 'change something'
end

我设法告诉matlab我希望做的事,但是这篇文章的重点是:我如何告诉matlab忽略字符串中A2P3之后字符串长度可变的内容?因为否则的话,查找保留在A2P3(以及B2P3等)上的每一个字符串信息将非常累人.

I manage to tell matlab to do as i wish, but the whole point of this post is: how do i tell matlab to ignore what comes after A2P3 in the string, where the string length is variable? because otherwise it would be very tiring to look up every single piece of string information left on A2P3 (and on B2P3 etc) just to say thay.

我该怎么做?

推荐答案

假定您正在使用代码中列出的T(单元格数组),则可以使用此代码来检测成功的匹配项-

Assuming you are working with T (cell array) as listed in your code, you may use this code to detect the successful matches -

%%// Slightly different than yours
T={'A2P3';'NotA2P3';'A2P3';'A2P3 with (extra1)';'A2P3 with (extra1) and (extra 2)';'A2P3 with (extra1)';'B2P3';'B2P3';'NotA2P3';'B2P3 with (extra 1)';'A2P3'};
a={1 1 0 1 1 0 1 1 0 1 1 }

T(:,2)=num2cell(1);
T(3,2)=num2cell(0);
T(6,2)=num2cell(0);
T(9,2)=num2cell(0);

%%// Get the comparison results
col1_comps = ismember(char(T(:,1)),'A2P3') | ismember(char(T(:,1)),'B2P3');
comparisons = ismember(col1_comps(:,1:4),[1 1 1 1],'rows').*cell2mat(T(:,2))

这篇关于通过检查前几个字符来识别相关的字符串信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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