0

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

UPDATES:

  • I will update this section as soon as I know how to copy my workspace into a code format

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

*** 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...

Here is what my data looks like:

http://imgur.com/H2exXKO

I want to have this:

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

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.

How do I do that?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • Can we assume a cell array with 2 columns instead? – Divakar Apr 21 '14 at 10:38
  • i could but it would make it harder for me to transform it back into my original table structure. how would your suggestion look like? im especially interested about the wildcard with various lengths, something to say "abc is important in abc11one". so if abc is in the first part, its okay already. – Easyquestionsonly Apr 21 '14 at 11:01
  • for finding text in tables im using categorical, but im curious abotu your implementation of wildcards in this situation. – Easyquestionsonly Apr 21 '14 at 11:11
  • In your uploaded screenshot you have only one column. – Divakar Apr 21 '14 at 12:15
  • the problem is i am running a script which goes on for hours and wanted to quickly post the most important information, therefore the 1 and 0s are not in the picture. i am sorry for that, but i can post an update of it tomorrow. also, it would help if you told me how you extract nested structures out of their environment and place them here into the web. i heard about two ways, one was a built in feature of 2014a which i dont have, the other some long script which i dont understand. – Easyquestionsonly Apr 21 '14 at 18:49

2 Answers2

1

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))
Divakar
  • 218,885
  • 19
  • 262
  • 358
  • that works for me. very smart to compare the first characters only and let the others be 0. I like how modularly it can be adapted to use also the first 6 characters etc. many thanks! – Easyquestionsonly Apr 22 '14 at 08:57
0

One quick solution would be to make a function that takes 2 strings and checks whether the first one starts with the second one.

Later Edit:

The function will look like this:

for i = 0, i < second string's length, i = i + 1
    if the first string's character at index i doesn't equal the second string's character at index i
        return false
after the for, return true

This assuming the second character's lenght is always smaller the first's. Otherwise, return the function with the arguments swapped.

Valdrinium
  • 1,398
  • 1
  • 13
  • 28
  • how? the strings are not always equally long. this is what im looking for, some kind of wildcard which can have any length. – Easyquestionsonly Apr 21 '14 at 10:34
  • thank you but what im looking for is not a simple length classifier of my string, im trying to ignore too long strings with the same beginning. i updated my first question. – Easyquestionsonly Apr 21 '14 at 11:13