Coding Practice: When to use a structure rather than separate variables?

조회 수: 38 (최근 30일)
Daniel Bridges
Daniel Bridges 2018년 1월 30일
댓글: Daniel Bridges 2018년 1월 30일
If dealing with x,y,z coordinates and parameters for each Cartesian axis, is there any benefit to using a structure rather than separate variables or even a 3x1 vector?
"Instinctively", it seems good to store it in a structure for the sake of tidiness, but is it wasting memory? Does a structure with three fields occupy more memory than three variables? I would think so, because the field names are stored in addition to the structure name. But if I make three variables rather than one structure, doesn't that clutter the Workspace? How do you decide in what manner to store and manipulate values?
For greater context, the following is a snippet of code I'm working on (adding to a function called from within a function, I wonder why bother storing negative margin values tidily in a structure if the function is going to clear it from memory after finishing anyway):
[PixelsToAdd.x,NegMarginX] = DeterminePixelsToAdd(RandomMotion.x,mmperpixel,DistFromCenterToPixelEdge);
[PixelsToAdd.y,NegMarginY] = DeterminePixelsToAdd(RandomMotion.y,mmperpixel,DistFromCenterToPixelEdge);
[PixelsToAdd.z,NegMarginZ] = DeterminePixelsToAdd(RandomMotion.z,mmperpixel,DistFromCenterToPixelEdge);
  댓글 수: 1
Daniel Bridges
Daniel Bridges 2018년 1월 30일
I think that I am on the right track in using structures, since it becomes easy to check parameters:
K>> RandomMotion
RandomMotion =
struct with fields:
x: [-1.8046 1.3639]
y: [2.8735 -0.7147]
z: [0.1994 -3.2835]
K>> PixelShifts
PixelShifts =
struct with fields:
x: [-1 1]
y: [1 0]
z: [-1 -3]
K>> Origin
Origin =
struct with fields:
x: 2
y: 1
z: 3

댓글을 달려면 로그인하십시오.

답변 (1개)

James Tursa
James Tursa 2018년 1월 30일
편집: James Tursa 2018년 1월 30일
I prefer using a single double matrix to store the x-y-z data. E.g. storing each x-y-z triplet as a column, which makes it easier to do things like rotation downstream with a simple matrix multiplication. It is easy to pick off the x or y or z data with indexing also (although that does result in a deep data copy). But it kinda depends on how the downstream code is set up to use the data.
To answer your questions directly:
"... is there any benefit to using a structure rather than separate variables or even a 3x1 vector?"
For code neatness, sure a structure can make the code neater. It does cost a little extra memory for the struct itself, but not much (only about 120 bytes). And maybe slightly more processing to extract the x-y-z data when it gets used, but again probably not that much extra. A better question might be how is your downstream code using the variables, because that might dictate how you should be storing the data.
"Does a structure with three fields occupy more memory than three variables?"
Yes. Per above, it costs you about 120 extra bytes for the struct variable itself. This is insignificant and should not be a deciding factor at all in your coding decisions. The field names also cost slightly more memory, but this is even less significant that the struct overhead. Again, the extra memory for all of this is completely insignificant compared to the memory you have on your computer. Don't sweat it.
"But if I make three variables rather than one structure, doesn't that clutter the Workspace?"
It can. But how much more clutter is there to having one struct variable called mydata vs having three variables called mydata_x, mydata_y, and mydata_z? Not much IMO, but it may depend on how many different sets of x-y-z data you have in your workspace.
"How do you decide in what manner to store and manipulate values?"
I would say it depends on what code you have downstream to process the data. If you are constantly having to extract the individual x-y-z values from your variables to combine them into single x-y-z triplet variables for processing, then I would say you are storing your data poorly and a single matrix with x-y-z triplets in columns would be better and easier to extract and process. But if you already have tested & working code that uses the x-y-z data as individual variables, then the struct or three variable approach might be better for you.
  댓글 수: 2
Stephen23
Stephen23 2018년 1월 30일
편집: Stephen23 2018년 1월 30일
"E.g. storing each x-y-z triplet as a column, which makes it easier to do things like rotation downstream with a simple matrix multiplication"
What is really neat about MATLAB is that with very simple syntax it is possible to define matrices and then apply transforms to them, e.g. rotation, translation, least-squares solution to systems of equations, etc. etc. All of these rely on data being in matrix form, and so the convenience of this cannot be underemphasized!
Daniel Bridges
Daniel Bridges 2018년 1월 30일
Because it is important that others can read my code to verify its functionality, I want to make variable names explicit. For example, RandomMotion.y cleary signifies random motion in the y axis in a way that RandomMotion(2) does not. (I.e. the reader must look elsewhere for context to verify that 1,2,3 refers to x,y,z.)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Naming Conventions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by