From: Gary on
Here is an example on what I'm trying to figure out:

function [out1, out2] = testfunc1(a,b,mat1)
out1 = a+b;
out2 = sum(mat1(1,:)); %sum of matrix first row
end

Suppose I have:
a=2;
b=3;
A=[1,2,3,4];
Then: [o1,o2]=testfunc1(a,b,A) would easily show:
o1=5
o2=10

Now I'm trying to pass arrays and cell arrays to the inputs, for example:
a=1:2;
b=4:5;
B=cell(2,1);
B{1}=[0.5,1.5,2.5,3.5; 1,2,3,4];
B{2}=[1,2,3,4];
B contains 2 matrices with different dimensions.

I tried [o1,o2]=testfunc1(a,b,B{1:2}), and I was hoping to get results:
o1 = [5 7]
o2 = [8 10]
However, o1 worked and o2 didn't work. The error mesg is:
"??? Error using ==> testfunc1
Too many input arguments."

How can I get this work? I tried for-loop, but with the array and cell dimensions really big, the loop took forever...

I also checked cellfun, but it takes only cell array inputs. My function needs both vector and cell array as inputs.

Many thanks ahead!
From: Steven_Lord on


"Gary " <zhifangli(a)yahoo.com> wrote in message
news:i414mj$6n5$1(a)fred.mathworks.com...
> Here is an example on what I'm trying to figure out:
>
> function [out1, out2] = testfunc1(a,b,mat1)
> out1 = a+b;
> out2 = sum(mat1(1,:)); %sum of matrix first row
> end
>
> Suppose I have:
> a=2;
> b=3;
> A=[1,2,3,4];
> Then: [o1,o2]=testfunc1(a,b,A) would easily show:
> o1=5
> o2=10
>
> Now I'm trying to pass arrays and cell arrays to the inputs, for example:
> a=1:2;
> b=4:5;
> B=cell(2,1);
> B{1}=[0.5,1.5,2.5,3.5; 1,2,3,4];
> B{2}=[1,2,3,4];
> B contains 2 matrices with different dimensions.
>
> I tried [o1,o2]=testfunc1(a,b,B{1:2}), and I was hoping to get results:

The syntax B{1:2} creates a comma-separated list, and so it's the equivalent
of:

[o1,o2]=testfunc1(a,b,B{1}, B{2})

Since your testfunc1 function is only defined to accept 3 inputs, you can't
call it with 4.

> o1 = [5 7]
> o2 = [8 10]

So you want o2 to have its first element be the sum of the first row of the
first cell in B and its second element be the sum of the first row of the
second cell?

> I also checked cellfun, but it takes only cell array inputs. My function
> needs both vector and cell array as inputs.

Then check the CLASS of the input. If mat1 is a numeric array, SUM it
directly. If it's a cell array, use CELLFUN.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com