assignment of an object to a vector
Show older comments
Hi all and happy new year!
I have the following simple problem. I have a function say
res=myfunc(a,b,c)
res=zeros(2,1);
res(1)=a+log(b)+cos(c);
res(2)=(b-c)/a;
end
in addition, I have a class say "myclass". When I call the function myfunc with numerical values for a, b and c, the result is as expected. However, when I call myfunc with inputs of class myclass, I get an error saying: "??? In an assignment A(I)=B, the number of elements in B and I must be the same"
clearly, numel(res(1)) is 1 and numel(a+log(b)+cos(c)) is also 1 although res(1) and a+log(b)+cos(c) are of different classes.
A simple workaround is to write the function above differently
res=myfunc2(a,b,c)
res=[
a+log(b)+cos(c)
(b-c)/a
];
end
that is, rather that declaring a vector as in the first case, I construct a vector directly. In this case things work fine as well. I just don't know how to make the code work for the first case as well.
Any thoughts?
Thanks, Pat.
Accepted Answer
More Answers (0)
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!