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:
Help for writing the script

Subject: Help for writing the script

From: Jerry

Date: 9 Nov, 2013 21:33:06

Message: 1 of 8

Hi everyone,

There are a three-column dataset (reference data), in that within the first column there are numbers, whereas within the second and third columns there are texts, please see the below:

RD=
USBarcodes EuropeBarcodes CARs
87937234 100424889_U_C BMWu
60937234 73424889_C_A Vwu
54237234 66724889_M_A Nissanu
36237234 48724889_K_Y Hondau
98537234 111024889_W_A Toyotau
98537234 111024889_B_C Mazdau
96337234 108824889_O_N Audiu
75337234 87824889_T_X BMWe
15937234 28424889_G_E Vwe
98737234 111224889_N_E Nissane
82537234 95024889_P_O Hondae
71437234 83924889_H_O Toyotae
60937951 73425606_J_A Mazdae
60937312 73424967_C_H Audie

I do have a vector data that includes some USbarcodes that are numbers for example:

VD=
60937234
36237234
96337234
15937234
82537234

I need to use this vector to find out related EuropeBarcodes and CARs from reference data.

OD=
60937234 73424889_C_A Vwu
36237234 48724889_K_Y Hondau
96337234 108824889_O_N Audiu
15937234 28424889_G_E Vwe
82537234 95024889_P_O Hondae

May someone help me to write a script that allows me to do this task within MATLAB? Thank you.

Jerry

Subject: Help for writing the script

From: dpb

Date: 10 Nov, 2013 17:04:00

Message: 2 of 8

On 11/9/2013 3:33 PM, Jerry wrote:
...
> There are a three-column dataset (reference data), in that within the
> first column there are numbers, whereas within the second and third
> columns there are texts, please see the below:
>
> RD=
> USBarcodes EuropeBarcodes CARs
> 87937234 100424889_U_C BMWu
> 60937234 73424889_C_A Vwu
...
> 60937312 73424967_C_H Audie
>
> I do have a vector data that includes some USbarcodes that are numbers
> for example:
>
> VD=
> 60937234
> 36237234
> 96337234
> 15937234
> 82537234
>
> I need to use this vector to find out related EuropeBarcodes and CARs
> from reference data.
...

doc ismember % and friends

--

Subject: Help for writing the script

From: Jerry

Date: 10 Nov, 2013 18:29:08

Message: 3 of 8

Thank you for your reply. When I tried “ismember”, I got following error:

[n,t,RD]=xlsread ('CAR');

%%
VD= [60937234
36237234
96337234
15937234
82537234];

%%
OD = ismember(VD,RD);

***********************************
Error using cell/ismember>cellismemberR2012a (line 199)
Input A of class double and input B of class cell must be
cell arrays of strings, unless one is a string.

Error in cell/ismember (line 71)
    [varargout{1:nlhs}] = cellismemberR2012a(varargin{:});
***********************************
% By converting VD to a string I got this error:

sVD = num2str(VD);
OD = ismember(sVD,RD);

***********************************
Error using cell/ismember>cellismemberR2012a (line 199)
Input A of class char and input B of class cell must be cell
arrays of strings, unless one is a string.

Error in cell/ismember (line 71)
    [varargout{1:nlhs}] = cellismemberR2012a(varargin{:});
***********************************

May someone let me know what I should do to get OD? Thank you.

Jerry

Subject: Help for writing the script

From: dpb

Date: 10 Nov, 2013 19:14:48

Message: 4 of 8

On 11/10/2013 12:29 PM, Jerry wrote:
> Thank you for your reply. When I tried “ismember”, I got following error:
>
> [n,t,RD]=xlsread ('CAR');
>
> %%
> VD= [60937234
> 36237234
> 96337234
> 15937234
> 82537234];
>
> %%
> OD = ismember(VD,RD);
>
> ***********************************
> Error using cell/ismember>cellismemberR2012a (line 199)
> Input A of class double and input B of class cell must be
> cell arrays of strings, unless one is a string.
...

> May someone let me know what I should do to get OD? Thank you.

Use the numeric results instead of the RAW text.

You'll want just one lookup instead of all probably, however,

Something like

t(n==VD(1))

maybe.

You'll need to look at the actual data as returned by xlsread() and
decide which it is you want to use and how to address it properly for
your purposes. In your form as written RD is just the raw text of the
input file in a cell array of strings--not terribly useful in that form.

--

Subject: Help for writing the script

From: Jerry

Date: 10 Nov, 2013 20:47:18

Message: 5 of 8

Thank you for your comments. I am not sure I got your point, however here is the results based on my understanding.

> Use the numeric results instead of the RAW text.

OD = ismember(VD,n)

OD =

     1
     1
     1
     1
     1

> t(n==VD(1))

t(n==VD(1))

ans =

    {''}

Thanks again,
Jerry

Subject: Help for writing the script

From: dpb

Date: 10 Nov, 2013 21:19:29

Message: 6 of 8

On 11/10/2013 2:47 PM, Jerry wrote:

......[top-posting repaired -- don't do that; hard conversation follow
makes]...

Also, you wiped out attributions...and the preceding code so can't see
how you actually did the xlsread() call again w/o going back and looking
at the other response.

Snipping judiciously is fine and good; be certain not to overdo it, however.

...

[dpb previously suggested]

>> Use the numeric results instead of the RAW text.

> Thank you for your comments. I am not sure I got your point, however
> here is the results based on my understanding.
>
> OD = ismember(VD,n)
>
> OD =
>
> 1
> 1
> 1
> 1
> 1
>
> t(n==VD(1))
>
> ans =
> {''}
>

OK, the ismember() result is what I expected since they are all included
in the set.

>> [n,t,RD]=xlsread ('CAR');

Now, what is the result in 't' after xlsread()? It should be a cell of
the array containing the second two columns as string arrays I'd
think...what does

whos t

give? The empty result looks like something didn't parse correctly/as
expected, maybe...

The expression n==VD(1) should return a logical vector of length(VD) w/
a T for the second element and that should then index into the cell
array t and return that element.

--

Subject: Help for writing the script

From: Jerry

Date: 10 Nov, 2013 22:45:13

Message: 7 of 8

Thank you for your comment. You are right.

> Now, what is the result in 't' after xlsread()? It should be a cell of
> the array containing the second two columns as string arrays I'd
> think...what does
>
> whos t

here is t:

'USBarcodes' 'EuropeBarcodes' 'CARs'
'' '100424889_U_C' 'BMWu'
'' '73424889_C_A' 'Vwu'
'' '66724889_M_A' 'Nissanu'
'' '48724889_K_Y' 'Hondau'
'' '111024889_W_A' 'Toyotau'
'' '111024889_B_C' 'Mazdau'
'' '108824889_O_N' 'Audiu'
'' '87824889_T_X' 'BMWe'
'' '28424889_G_E' 'Vwe'
'' '111224889_N_E' 'Nissane'
'' '95024889_P_O' 'Hondae'
'' '83924889_H_O' 'Toyotae'
'' '73425606_J_A' 'Mazdae'
'' '73424967_C_H' 'Audie'

as I mentioned, the first column is not a text column (but a number column); it was why it was not appear in the t. Is there anyway to have the first column along with t?

> The expression n==VD(1) should return a logical vector of length(VD) w/
> a T for the second element and that should then index into the cell
> array t and return that element.

Here is:

n==VD(1)

ans =

     0
     1
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0
     0

Please let me know what I could do, Thank you, Jerry

Subject: Help for writing the script

From: dpb

Date: 11 Nov, 2013 07:06:50

Message: 8 of 8

On 11/10/2013 4:45 PM, Jerry wrote:
> Thank you for your comment. You are right.
>
>> Now, what is the result in 't' after xlsread()? It should be a cell of
>> the array containing the second two columns as string arrays I'd
>> think...what does
>>
>> whos t
>
> here is t:
>
> 'USBarcodes' 'EuropeBarcodes' 'CARs'
> '' '100424889_U_C' 'BMWu'
> '' '73424889_C_A' 'Vwu'
> '' '66724889_M_A' 'Nissanu'
...

That's as expected, ok...but, I'd eliminate the first title row for
convenience as well as the first row of 'n'. They're really of no hep
for anything.

But, it's not the full info I asked for -- what's

whos t

give? That's the dimensions that needed to know how to reference what
it is you're interested in out of it for any given piece you desire.

> as I mentioned, the first column is not a text column (but a number
> column); it was why it was not appear in the t. Is there anyway to have
> the first column along with t?

Why? That's what's useful...

>> The expression n==VD(1) should return a logical vector of length(VD)
>> w/ a T for the second element and that should then index into the cell
>> array t and return that element.
>
> Here is:
>
> n==VD(1)
>
> ans =
>
> 0
> 1
> 0
> 0
> 0
...

Again, that's correct.


> Please let me know what I could do, Thank you, Jerry

So,

t(n==VD(1))

definitely should give you the second row of t or

  '' '100424889_U_C' 'BMWu'

Altho I guess probably t is nx3 so you want

t(t(n==VD(1),2:end)

for the second two columns.

Again, I'd wipe that first column of empty strings and the first row out
as well as the first row of 'n' to make them commensurate in sizes and
eliminate the superfluous headers.

It doesn't appear you're really digging in to try to understand what
you're doing here but just waiting for somebody to solve your problem.

Read up on cell arrays and addressing them as well as logical addressing
and the find() function as well as the other intersection functions
ismember() and friends. They're the toolset to deal with the questions
you've raised.

--

--

Tags for 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