Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MATLAB - multiple return values from a function?

I'm writing 2 functions in matlab, an initialize function and a function to insert items into an array treating it like a doubly-linked list. However, my initialize function only returns "ans =" and the initialized array. How can I have it also set values of my other variables? Here's my code:

function [ array, listp, freep ] = initialize( size )     array = zeros(size, 3);     listp = 0;     freep = 1; end 
like image 945
Nick Avatar asked Nov 15 '10 19:11

Nick


People also ask

Can you return multiple values from a function?

We can return more than one values from a function by using the method called “call by address”, or “call by reference”. In the invoker function, we will use two variables to store the results, and the function will take pointer type data.


1 Answers

Matlab allows you to return multiple values as well as receive them inline.

When you call it, receive individual variables inline:

[array, listp, freep] = initialize(size) 
like image 164
Mikhail Avatar answered Oct 11 '22 14:10

Mikhail