Saving return values of function returning multiple variables in Matlab

I have never used matlab before so excuse this very basic question.

Basically I have a function that returns multiple variables, defined like so:

function [a, b, c]=somefunction(x, y, z)

I know I can get the return values as follows:

[a,b,c] = somefunction(1,2,3);

Now what I would like to do instead is save multiple runs of somefunction into an array and then retrieve them later. I tried:

results = [];
results = [results somefunction(1,2,3)];
results = [results somefunction(4,5,6)];

And then I tried accessing the individual runs as:

% access second run, i.e. somefunction(1,2,3) ?
a = results(2, 1);
b = results(2, 2);
c = results(2, 3);

but this tells me that the index is out of bound because size(results) = [1,99654] (99654 is the number of results I need to save). So it does not appear to be an array? Sorry for this basic question, again I have never used matlab.

Answers


When you combine arrays with [ ... ], you're concatenating them, creating one long flat array. For example, if call 1 returns 3 elements, call 2 returns 8 elements, and call 3 returns 4 elements, you'll end up with a 14-long array, and no way of knowing which elements came from which function call.

If you want to keep the results from each run separate, you can stash them in a cell array. You still need a comma-separated list on the LHS to get all the multiple argouts. The {}-indexing syntax, as opposed to (), "pops" contents in and out of cell elements.

Let's store the results in a k-by-n array named x, where the function returns n outputs and we'll call it k times.

x = cell(2, 3); % cell(k, n)
% Make calls
[x{1,1}, x{1,2}, x{1,3}] = somefunction(1,2,3);
[x{2,1}, x{2,2}, x{2,3}] = somefunction(4,5,6);
% Now, the results of the ni-th argout of the ki-th call are in x{ki,ni}
% E.g. here is the 3rd argout from the second call
x{2,3}

You could also store the argouts in separate variables, which may be more readable. In this case, each will be a k-long vector

[a,b,c] = deal(cell(1,2));  % cell(1,k)
[a{1}, b{1}, c{1}] = somefunction(1,2,3);
[a{2}, b{2}, c{2}] = somefunction(1,2,3);

And of course this generalizes to loops, if your somefunction inputs are amenable to that.

[a,b,c] = deal(cell(1,nIterations));
for k = 1:nIterations
    [a{k}, b{k}, c{k}] = somefunction(1,2,3);
end

Details are in the doco at http://www.mathworks.com/help/matlab/cell-arrays.html or doc cell.

(Side note: that results(1, 2) in your post ought to succeed for an array of size [1,99654]. Sure you didn't do results(2, 1)?)


Need Your Help

Condition Inside Count Function Using Case In Sql Server

sql-server

I have one table having two columns named OrderId and OrderType

How to delete files from directory on specific time, daily?

php file delete-file

My data.txt is generated each time when someone is visiting my site. I would like to delete this file on on specific time, let say 1:00AM daily.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.