1

I would like to comapre two arrays of strings how could I affect the following values to a,b,c,d , when I try as below, I got an error

[a,b,c,d] = getVal(x);
    =>this will gives :
a =

a
b=

0
c =

10
d =
[]   

and I have :

expected = {'a','0','10',[]};

how could I make the comparison between [a,b,c,d] and expected ?

2

2 Answers 2

2

Mistake 1:

= is the assignment operator.

The comparison operator is ==.


Mistake 2:

MATLAB arrays don't generally hold strings. They hold numbers or single characters.

>> b = ['a','0','10','20']

b =

a01020

To see why [a,b,c,d] = ['a','0','10','20'] doesn't work, consider this:

>> [a,b,c,d] = 'a01020'
??? Too many output arguments.

You're trying to put six characters into four buckets. Not going to work.

You might have meant to create a cell array:

>> c = {'a','0','10','20'}

c = 

    'a'    '0'    '10'    '20'

Matlab arrays are numerical matrices, not general-purpose list containers.

Sign up to request clarification or add additional context in comments.

2 Comments

what I want to do is to affect each string to a value from table
@lola - Can you post the exact text of the error you are getting? Also, if you want to show values in a column rather than a row, then do c' (the apostrophe ' means transpose)
2

Following on from Li-aung's answer, what you probably want is something like

isequal({a,b,c,d}, {'a', '0', '10', '20'})

This will return true iff a has the value 'a' and so on.

EDIT

To perform multiple assignments, you can use DEAL

[a,b,c,d] = deal('a', '0', '10', '20')

1 Comment

I think we have a very strong language barrier in effect. By compare she really means assign.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.