MATLAB Answers

Efficient Means of Emulating a Pointer

1 view (last 30 days)
Hello All,
I am writing a program wherein I am using 2 main structs to pass data to function & subfunctions. Depending on the dataset 'well' could be 1x1 up to 1x16 and 'stage' could be 1x1 up to 1x100+
well = 1x3 struct array containing the fields:
name
coords
pp
x
stageNums
stageNumbersIndices
perfCoordinates
stageNames
stage = 1x68 struct array containing the fields:
treatmentWell
wellOrder
name
num
E
N
Z
time
conf
mag
color
slope
azimuth
line
center
EE
NN
ZZ
Some things I was considering here as I clean up the code for beta testing within my group:
1. A sensible parent-child relationship would be 'well'>'stage', with 'stage' as a nested struct within 'well' but that would alter the IO flow of the program as the user interacts with the GUI & plots, so I can't do that without a serious overhaul (not out of the question, but not happening this week). A lazy (but effective) practice would be to nest the correct 'well' struct into every cell of the 'stage' struct but it seems very inefficient.
2. Right now I am using the dummy 'wellOrder' which is an index (in this case 1, 2 or 3) to the proper cell in the 'well' struct and passing both the 'well' and 'stage' structs to each function. Ideally I could pass a pointer to 'well' in the 'stage' struct, but it doesn't seem possible within MATLAB, does anyone know of an efficient way to emulate a pointer in MATLAB?

Accepted Answer

Walter Roberson
Walter Roberson on 29 May 2015
For situations in which there are no circular references, cell arrays are the closest to pointers. Because that's pretty much what they are, arrays of pointers to items, where (as usual in MATLAB) items have information about data type and array sizes. (Every item in MATLAB has that. Even literal constants are converted to that form.)
If you want efficiency of representation then arrays and index values are the way to go because that only requires the MATLAB-level type and size information once. But it might require that you keep information on the size yourself, and as you dynamically create and delete objects you need to do all of the management yourself, finding space in the array, marking space as free, and so on. Due to those overheads and the possibility of getting them wrong, it is not usually a wise use of programmers time to deal with those for dynamic structures -- though it might be worth it for cases where the information is built only once and used many times. Even then, in these days where memory is cheap compared to people, I would only consider doing it myself for... ummmm, actually for situations where I would not be using MATLAB at all (unless perhaps using it to generate C/C++ code that was then to be hand-tuned.)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by