Hi I'd like to know whether it's possible to pass variables by reference in Matlab like it is in C (i.e, using the & operator to actually pass the address). A classic example would be a function to swap two numbers? Prasenjit |
|
Passing variables by reference
Started by ●September 15, 2001
Reply by ●October 11, 20012001-10-11
Hi, There is nothing in native MATLAB that is like 'pass by reference' and of coarse no special sign for it. The most close approach to this is a combination of two functions: 1) inputname, 2)assignin('caller','..') For example: function dbl(chucubucu,y) A=inputname(1); B=2*y; assignin('caller',A,B) If you call this function like that: y; x=1; dbl(x,y) x x = 20 Not that chucubucu is a dummy variable I hope this help Joe BSTEX --- Prasenjit Sengupta <> wrote: > Hi > > I'd like to know whether it's possible to pass > variables by reference > in Matlab like it is in C (i.e, using the & operator > to actually pass > the address). A classic example would be a function > to swap two > numbers? > > Prasenjit > |
|
Reply by ●October 11, 20012001-10-11
Prasenjit Sengupta- While not having the ability to "pass parameters by reference" to functions, MATLAB has the neat ability to return multiple values: [a, b] = swap(a, b); function [a, b] = swap(a_in, b_in) a = b_in; b = a_in; %end Jeff Brower DSP/telecom engineer Signalogic On Wed, 10 Oct 2001, Joe Sababa <> wrote: >Hi, >There is nothing in native MATLAB that is like 'pass >by reference' and of coarse no special sign for it. >The most close approach to this is a combination of >two functions: 1) inputname, 2)assignin('caller','..') >For example: >function dbl(chucubucu,y) >A=inputname(1); >B=2*y; >assignin('caller',A,B) > >If you call this function like that: >y; >x=1; >dbl(x,y) >x >x = > 20 >Not that chucubucu is a dummy variable >I hope this help >Joe >BSTEX >--- Prasenjit Sengupta <> wrote: >> Hi >> >> I'd like to know whether it's possible to pass >> variables by reference >> in Matlab like it is in C (i.e, using the & operator >> to actually pass >> the address). A classic example would be a function >> to swap two >> numbers? >> >> Prasenjit |