A pointer (or reference) in Matlab

Version 1.0.0.0 (1.29 KB) by Andrew
Get the behavior of a pointer.
258 Downloads
Updated 22 Mar 2013

View License

Create a pointer:
o = ptr();
Do anything you want with it:
o.d.msg = 'hi';
o.d.A = randn(10);
p = o;
p.d.msg = 'hello';
disp(o.d.msg); % 'hello'
Think of '.d.' as the C language's '->' operator. Write a function that acts on it:
function DoSomething(o)
o.d.A(end,end) = 10;
end
Make sure to delete it:
delete(o);
Since o and p point to the same object, p is now invalid just like o.

Cite As

Andrew (2026). A pointer (or reference) in Matlab (https://www.mathworks.com/matlabcentral/fileexchange/40907-a-pointer-or-reference-in-matlab), MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux
Categories
Find more on Desktop in Help Center and MATLAB Answers
Version Published Release Notes
1.0.0.0