0

Possible Duplicate:
how to compare array with matlab

Here's an example of what I'm looking for:

[a,b,c,d] = getVal(x);

This will give:

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 ? When I call only getVal(x), it gives me only the first value and when I write:

[a,b,c,d] = getVal(x)

Then I got all values in the log. Why isn't this the case when I call
getVal(x)? For comparison I tried:

isequal([a,b,c,d], expected {1:end})

but it doesn't work, any idea how to solve my problem?

Community
  • 1
  • 1
lola
  • 5,649
  • 11
  • 49
  • 61
  • you could've just edited your previous question and continued there: http://stackoverflow.com/questions/10533761/how-to-compare-array-with-matlab – Gunther Struyf May 10 '12 at 13:32

1 Answers1

0

Don't try to put a, b, c, d into a matrix. That would append spaces. Instead, use a cell array, just like you have for expected:

>> a='a'; b='0'; c='10'; d=[];
>> expected = {'a','0','10',[]};
>> isequaln({a,b,c,d}, expected)

ans =

     1

You also asked why simply calling getVal(x) doesn't give all the values. That is because in MATLAB, a function can (and often does) react to the number of output parameters, i.e., the number of variables on the left hand side of the assignment. Your getVal function returns four values if called with four output parameters; If it doesn't do anything special, then calling it with zero or one output parameter will return only the first of these values, in your example, 'a'. If you want a cell array with all four of these values, do something like

[a, b, c, d] = getVal(x)
{a, b, c, d}
Christopher Creutzig
  • 8,656
  • 35
  • 45
  • I'm using a function getVal(x) witch returns [a,b,c,d], and I want to compare this to expected – lola May 10 '12 at 13:38
  • So? The comparison step is not affected by how a though d got their values. (Just to be on the safe side: “compare” means “take two things and check if they are equal.”) – Christopher Creutzig May 10 '12 at 13:40
  • in your proposed solution you are construncting two expected values whereas I want to compare expected to the actual value returned by function – lola May 10 '12 at 13:42
  • Just replace the first line by the first line of your code above. It doesn't matter where the values come from. – Christopher Creutzig May 10 '12 at 13:44
  • I've tried : [a, b, c, d] = getVal(x) ; then isequal({a,b,c,d}, expected) and this seems working. but I can't do directly : {a, b, c, d} = getVal(x)? Thanks Christopher – lola May 10 '12 at 13:49
  • No, you can't. The left hand side of an assignment cannot be a cell array of variables. (In case you plan to compare doubles, check out the difference between `isequal` and `isequaln`.) – Christopher Creutzig May 10 '12 at 13:52