1

I have a 1 x 118 matrix called current_load that I need to update periodically. This matrix resides in the main workspace of Matlab (as shown in the code bellow).

current_loads = zeros(1, 118); 
for col=1:118
    current_loads(1,col)=10; %// Initially give all nodes a current load of 10    
end
recursive_remove(current_loads); %calling function

This matrix will be passed to a function call recursive_remove (shown bellow).

function updater = recursive_remove( current_load )
     current_load(1,3) = 2.6; %// This update can't be seen from main ??
     %this function will be called recursively later
end

But whatever updates I do to this current_load matrix from the function, it will not get updated since I don't know how to pass it by reference.

I am new to Matlab. I would greatly appreciate if you can show with an example how to handle this

Hoki
  • 11,637
  • 1
  • 24
  • 43
D P.
  • 1,039
  • 7
  • 27
  • 56

2 Answers2

2

EDIT: "How to pass parameter by reference in Matlab" You can solve your problem passing your arguments by reference

You need a handle class

Handle Classes

Objects that share references with other objects

this is, create a file called HandleObject.m with this code:

classdef HandleObject < handle
   properties
      Object=[];
   end

   methods
      function obj=HandleObject(receivedObject)
         obj.Object=receivedObject;
      end
   end
end

Then you can do something like this

Object = HandleObject(your matrix)
yourFunction(Object)

And inside your function

function yourFunction(myObject)
    myObject.object = new matrix;
end

With that you can achieve some kind of pass by reference and avoid getting a lot of copies trought your program.

lcjury
  • 1,158
  • 1
  • 14
  • 26
  • I was thinking exactly about that when I saw the title of the question, only to see that finally it wasn't actually the question. Nice answer anyway ! – Hoki Apr 27 '15 at 18:28
  • Why it wasn't?, he wanted to change the value of the matrix inside of a function. – lcjury Apr 27 '15 at 21:10
  • Well, in fairness the OP hinted in comment that that's what he was looking for. But from what I understand, he real title of the question should be more like "how to return a result from a function". This has little to do with how to **pass** argument to the function but rather how to return them. Anyway, I think your answer deserve to stay, but to make it more understandable now, may be you should edit it and add on top that you were initially answering "How to pass parameter by reference in Matlab" – Hoki Apr 27 '15 at 21:11
  • the real title of the question should be "please solve my problem", anything who make the code of the guy code works would be fine for him. anyway thanks ;) I will edit my question – lcjury Apr 27 '15 at 21:16
0

The output of the function recursive_remove hasn't been defined and so you ouput can't be used anywhere else.

In matlab you define outputs of functions with square brackets as below.

function [ output1, output2 ] = recursive_remove( input1, input2 )

The outputs can now be passed into other MATLAB docs - functions.

When calling the function in the example above in a different function as you did in your first bit of code you would call it as shown:

current_loads = zeros(1, 118); 
for col=1:118
    current_loads(1,col)=10; %Initially give all nodes a current load of 10    
end
[ output1, output2 ] = recursive_remove( input1, input2 ); %calling function

With this syntax you can take output1 and call it in the input of your next function recursive_remover

bilaly
  • 536
  • 3
  • 12
  • Thanks a lot for showing me with an example code. It's very clear now – D P. Apr 27 '15 at 17:34
  • just need to clear that this answer is pass by value, not pass by reference. – lcjury Apr 27 '15 at 17:39
  • thanks actually i always confuse the two just found this and it explained it really well, doubt i'll forget it now... http://stackoverflow.com/questions/373419/whats-the-difference-between-passing-by-reference-vs-passing-by-value – bilaly Apr 27 '15 at 17:45