MATLAB: Assign vectors (returned from a function) to the colums of a matrix

compact assignement of returned values

Let's suppose I have a function that returns three column vectors: "a", "b" and "c". (in this order) As of now I'm assigning these vectors to the columns of a matrix in the following way:
[A(:,1), A(:,2), A(:,3)] = myfun(…)
I would like to know if there is a more compact way to achieve this result, for example, something like this: [A(:,:)]=myfun(…)
And, if possible, I would like to extend this concept for the fields of a struct.

Best Answer

If you can modify the source code of myfun, you could have it detect the number of requested outputs. If the number is greater than 1, then return the individual columns. But if the number is 1 or less, return a matrix. E.g., the function code could be this:
function [a,b,c] = myfun
if nargout <=1
a = reshape(1:9,3,3);
else
a = (1:3)';
b = (4:6)';
c = (7:9)';
end
return
end
And the calling sequence would be:
>> [a b c] = myfun
a =
1
2
3
b =
4
5
6
c =
7
8
9
>> A = myfun
A =
1 4 7
2 5 8
3 6 9