How to change original variable inside function?
Show older comments
I want to change variable inside function, something simular to this c++ function:
void f(int &a){a+=2;}
I've heard handles can be useful here, but anything I tried only generated copy of variable, and was unable to change original. I'd use simple return values, but it's callback function and I can't return anything, at least as far as I know. Please help.
Accepted Answer
More Answers (1)
Image Analyst
on 9 May 2015
Simply pass back the variable in the output list
function a = doubleit(a)
a = 2 * a; % Change a.
Now, in the calling routine
a = 5
a = doubleit(a);
Now a will have the new value you set it to inside the function, 10 in this case);
6 Comments
Anoop Mysore
on 14 Jul 2016
this meathod is not working
Image Analyst
on 14 Jul 2016
Edited: Image Analyst
on 14 Jul 2016
Anoop, of course it works. To prove it:
function test
a = 16
a = doubleit(a) % A will become 32 now.
function a = doubleit(a)
a = 2 * a; % Change a.
Now look in my command window:
>> test
a =
16
a =
32
So it worked. a was 16 and now it's 32 so it got changed by the function. In fact it's the same answer Stephen gave, which you accepted.
Dillon Flannery-Valadez
on 26 Feb 2019
This method does not work. This is simply returning a new value, and it is not what the person asked for, which was changing the original variable inside a function. This is only changing a copy of the original variable and passing it back overwriting the last copy. The idea is to save space in memory and not create useless copies, which is what your answer does.
Image Analyst
on 26 Feb 2019
John said "I'd use simple return values, but it's callback function and I can't return anything, at least as far as I know." So I gave him a way to "use simple return values" like he said he'd be willing to do, but didn't know how to return things yet.
(Mentioning that it was a callback function didn't make any sense since there is no "original" being passed into a callback function nor is there any return value from a callback function since there is no place to accept any returned value even if it could. The correct way to share variables between callback functions is in the link Stephen gave).
The line
a = doubleit(a)
DOES change the original. "a" gets changed. True, it doesn't change it inside the function, but who cares, it changes it. This is the way call by value languages work, and it gets the job done. I presume they do it that way because it's a safer way.
John didn't say he wanted to save space in memory. The idea to "to save space in memory and not create useless copies" was your idea -- John did not mention anything about saving memory, and in these days of 32 GB of RAM, it probably won't even make any difference anyway, certainly not for any array less than a few hundred megabytes. I don't know the "fancy inbuilt stuff to intelligently minimize memory usage" that Stephen mentioned but I don't doubt it.
In short, the user asked for something that is not a feature of the language and we showed him how to get the same result (changing the original array) within the framework of the language. If he is really concerned about doing it inside the function, then he shouldn't be, unless he's dealing with monstrously-sized arrays. It won't make any difference for arrays that are a few elements, or a few million elements, in size.
Lunky Sucipto
on 11 May 2022
@Image Analyst In your doubleit example, you use 'a' as both input and output. However my case is different:
function object = find(k)
% find the object from my data structure that has property k
object = item_with_property_k
end
After finding the object, I now need to modify the object. How can I do this? I'm going to read libpointer now, but I'm not sure about anything right now.
Image Analyst
on 11 May 2022
@Lunky Sucipto just modify it. For example if it's a structure and you want to add a field "foundIt", just do
object.foundIt = true;
Categories
Find more on Use Prebuilt MATLAB Interface to C++ Library in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!