Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to check if a string matches any string in a matrix C

Subject: How to check if a string matches any string in a matrix C

From: Yukiko Shimizu

Date: 17 Jun, 2010 15:56:04

Message: 1 of 9

Hello.

Say I have type='xyz'
and C=['xyz' 'xxy 'zyx']
How do I check if type is an element of C? If it is I want it to return a logical 1.

Thank you.

Subject: How to check if a string matches any string in a matrix C

From: Rob Campbell

Date: 17 Jun, 2010 16:07:04

Message: 2 of 9

"Yukiko Shimizu" <yk.mizu@gmail.com> wrote in message <hvdgik$eoo$1@fred.mathworks.com>...
> Hello.
>
> Say I have type='xyz'
> and C=['xyz' 'xxy 'zyx']
> How do I check if type is an element of C? If it is I want it to return a logical 1.

I think you want to use a cell array not the concatenation you show above. Then you'd do:
C={'xyz' 'xxy' 'zyx'};
 strmatch('xxy',C)

ans =

     2

Subject: How to check if a string matches any string in a matrix C

From: Matt Fig

Date: 17 Jun, 2010 16:09:04

Message: 3 of 9

C = ['xyz' 'xxy' 'zyx']
type = 'xyz'
is_type_in_C = any(strfind(C,type))

Note that you may have wanted this instead because string concatenation in C:

C = {'xyz' 'xxy' 'zyx'}
any(ismember(C,type))

Subject: How to check if a string matches any string in a matrix C

From: Yukiko Shimizu

Date: 17 Jun, 2010 16:16:04

Message: 4 of 9

"Yukiko Shimizu" <yk.mizu@gmail.com> wrote in message <hvdgik$eoo$1@fred.mathworks.com>...
> Hello.
>
> Say I have type='xyz'
> and C=['xyz' 'xxy 'zyx']
> How do I check if type is an element of C? If it is I want it to return a logical 1.
>
> Thank you.

Wait, so what is the exact difference between { and [? I always used [ thinking I would be making a matrix.

Subject: How to check if a string matches any string in a matrix C

From: Matt Fig

Date: 17 Jun, 2010 16:22:21

Message: 5 of 9

"Yukiko Shimizu" <yk.mizu@gmail.com> wrote in message
> Wait, so what is the exact difference between { and [? I always used [ thinking I would be making a matrix.

Well, why not look at it?

% First this one.
C = {'xyz' 'xxy' 'zyx'};
C{1}
C{2}
C{3}
whos C

% Now this one.
C = ['xyz' 'xxy' 'zyx']
C(1:3)
whos C

Subject: How to check if a string matches any string in a matrix C

From: Rob Campbell

Date: 17 Jun, 2010 16:30:26

Message: 6 of 9


> Wait, so what is the exact difference between { and [? I always used [ thinking I would
>be making a matrix.

Yes, you're basically right but look in the Matlab programming docs for details. The catch is that the [ ] operator means "do concatenation" not "make matrix". So you could do t=[1,10,33] or t=[1,2,3]. But note that you can also do t=1:3, where you're making vector without the square brackets because nothing is being concatenated.

The { } means "construct cell array." I think of a cell array as an array of pointers to anonymous variables. A cell array isn't a matrix because you can do things like:
t={'A', ones(10), 'lskdfhjlkds'}
This is why it's possibly more suitable for what you want--each string can be a different length and they can't interact with each other.

Subject: How to check if a string matches any string in a matrix C

From: Yukiko Shimizu

Date: 17 Jun, 2010 16:49:06

Message: 7 of 9

Alright, thank you very much. Could you also explain what this means:

??? Output argument "qa" (and maybe others) not
assigned during call to "H:\dcm2quat.m>dcm2quat".

my input output function is:

function [qa,qb]=dcm2quat(A,type)

and I'm trying to just output qb since qa is not applicable in this case.

"Yukiko Shimizu" <yk.mizu@gmail.com> wrote in message <hvdgik$eoo$1@fred.mathworks.com>...
> Hello.
>
> Say I have type='xyz'
> and C=['xyz' 'xxy 'zyx']
> How do I check if type is an element of C? If it is I want it to return a logical 1.
>
> Thank you.

Subject: How to check if a string matches any string in a matrix C

From: Yukiko Shimizu

Date: 17 Jun, 2010 17:28:20

Message: 8 of 9

NM, Thank you all very much for your help.

"Yukiko Shimizu" <yk.mizu@gmail.com> wrote in message <hvdjm2$3c3$1@fred.mathworks.com>...
> Alright, thank you very much. Could you also explain what this means:
>
> ??? Output argument "qa" (and maybe others) not
> assigned during call to "H:\dcm2quat.m>dcm2quat".
>
> my input output function is:
>
> function [qa,qb]=dcm2quat(A,type)
>
> and I'm trying to just output qb since qa is not applicable in this case.
>
> "Yukiko Shimizu" <yk.mizu@gmail.com> wrote in message <hvdgik$eoo$1@fred.mathworks.com>...
> > Hello.
> >
> > Say I have type='xyz'
> > and C=['xyz' 'xxy 'zyx']
> > How do I check if type is an element of C? If it is I want it to return a logical 1.
> >
> > Thank you.

Subject: How to check if a string matches any string in a matrix C

From: Rob Campbell

Date: 17 Jun, 2010 18:08:21

Message: 9 of 9


> ??? Output argument "qa" (and maybe others) not
> assigned during call to "H:\dcm2quat.m>dcm2quat".
>
> my input output function is:
>
> function [qa,qb]=dcm2quat(A,type)
>
> and I'm trying to just output qb since qa is not applicable in this case.

It's not working because qa isn't being defined. You can do various things.
One is to define qa and qb as empty variables (qa=[ ]; qb= [ ]) right at the start of dcm2quat. That way you always get both outputs even though you may not use them.

Another thing you could do is to package all outputs of the function as a structure. e.g.
function out=dcm2quat(A,type)
 out.qb='something'
end
That way it won't matter that qa wasn't defined and it's easy to add more variables.

As a side note, you shouldn't call the variable "type". This is because there's already a function called "type" (see help type). Your variable names take priority over function names and so you can mask a function when you define variable with the same name. e.g.
>> rand(1,3)
ans =
   0.8147 0.9058 0.1270

>> rand=[ ];
>> rand(1,3)
??? Index exceeds matrix dimensions.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us