ISSHARED determines data sharing status among workspace variables

ISSHARED determines data sharing status among workspace variables
Updated 21 May 2018

View License

ISSHARED determines if a variable is sharing data with other variables. This sharing takes place automatically in MATLAB behind the scenes, and normally the user need not pay attention to it. However, if the user is working with large variables, it can be a huge benefit to understand how this sharing is taking place and what actions can cause shared data copies vs deep data copies in order to write code that better manages memory and timing. The types of sharing that can be detected by this mex routine are:

Shared Data Copy --> Separate header but data pointers are shared

Reference Copy --> Variable header & data pointers are shared.
Note: Reference copies can either be direct
or through a shared "parent". See examples.
E.g., struct field elements and cell elements
are often shared this way.

Looks for sharing among ordinary workspace variables, classdef object public properties, and function handle embedded data items. This mex routine cannot always tell when empty variables are shared reference copies of each other. E.g.,
>> x = 1:3
x =
1 2 3
>> y = x
y =
1 2 3
>> isshared(x,y)
ans =
>> y(1) = 4
y =
4 2 3
>> isshared(x,y)
ans =
In the above example, the assignment y = x caused the variable y to be created such that it was sharing the same data memory as x. But when we subsequently changed one of the elements of y, suddenly the real data memory of y points to a different place. This is known as copy-on-write. MATLAB does not make a deep data copy (i.e., copying the data to a brand new memory location) until it needs to. It didn't need to for the initial y = x assignment, but it did need to when one of the elements of y was changed. ISSHARED can be used to show this type of sharing for all of the variables in the current workspace.
In addition to simply reporting if input arguments are shared, ISSHARED can generate sharing name lists for single variables, or for all of the variables in the current workspace. E.g.,
[N,M] = isshared;
N = a vector containing the number of variables in each sharing group that were detected
M = a cell array of cell array of char strings containing the names of the sharing variables

Cite As

James Tursa (2024). ISSHARED determines data sharing status among workspace variables (, MATLAB Central File Exchange. Retrieved .

MATLAB Release Compatibility
Created with R2011a
Compatible with any release
Platform Compatibility
Windows macOS Linux
Find more on Workspace Variables and MAT-Files in Help Center and MATLAB Answers
Tags Add Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes

Updated for R2018a

Updated for 64-bit and later versions of MATLAB. Added classdef object public property and function handle data capability.