MATLAB: Does passing large structures, such as a GUI handles structure, to a function increase execution time

guihandlesMATLAB

In essence I often find when building GUI's that large amounts of my code are replicated and therefore I try to write functions which can be used multiple times.
An example of this are when I have multiple sliders with corresponding edit boxes where changing the value in one updates the other. I found the concisest way of writing the function was to pass the entire GUI handles structure to the function with a reference to which slider / edit pair I wanted to update.
However does passing a large number of variables (within the handles structure) that are essentially unused dramatically increase the execution time of the function / is it considered bad practice?
Appreciate this is a very general broad question with lots of different cases and therefore probably difficult to answer so thanks in advance 🙂

Best Answer

"In essence I often find when building GUI's that large amounts of my code are replicated and therefore I try to write functions which can be used multiple times."
Sounds quite reasonable. That is the recommended practice.
"However does passing a large number of variables (within the handles structure) that are essentially unused dramatically increase the execution time of the function / is it considered bad practice?"
I would say there is no problem passing the handles structure around. MATLAB does not copy the data just because you passed it to some other function (unless it is changed in the function, a feature known as copy on write) so there is unlikely to be any performance penalty for passing that data. Read this to know more:
In general copy on write means that normally you should just write your code without worrying about what is happening in the memory. Only if there is a clear problem should you try to deal with it (and currently you have not stated any particular problem, just a question asking if you are doing things in a good way.... which you are).
" I found the concisest way of writing the function was to pass the entire GUI handles structure to the function with a reference to which slider / edit pair I wanted to update..."
Perfect. The time saved by writing concise, easy to read, easy to understand, easy to debug code likely hugely outweighs the time you would spend trying to write some complex ways to cheat MATLAB's memory management. Do NOT fall into the trap of premature optimization!