I am looking for advice on the best way to approach this problem. My initial instinct was that pointers would be the way to go, but MATLAB doesn't have "normal" pointers and any pointer-like solutions I've seen are hacks using functions or classes derived from handles that I'm not sure would work particularly well in my situation. So perhaps I'm thinking about this all wrong and there's a cleaner way to do things from the start...
Here's the situation: I am doing thermodynamic type calculations in two closed fluid loops. For each loop I have a single structure variable that contains the various items in the loop. Let's suppose that one loop has a heat source and a heat exchanger in it and the other has a heat sink and a heat exchanger in it, forgetting about things like temperatures and neglecting all the other loop pieces to keep things simple. These loops are connected via that common heat exchanger which is a part of both loops.
So my calculations might be such that they effectively set the following items:
Now I'm ready to do some calculations in the second loop to get that heat load to the heat sink. Let's say I now want to calculate the heat exchanger's cold side flow rate (i.e. the loop 2 flow rate). These calculations belong to loop 2, yet the heat exchanger is the same one for both loops. So right now what I do is:
Then I do my calculation and arrive at:
Because these heat exchangers are actually the same piece, I then currently copy the structure back over to loop 1 so it's up to date.
Having to do these copies is an error prone approach. I would like to somehow establish a linking/pointer/alias type relationship between loop1.heatExchanger and loop2.heatExchanger such that I don't need to do any copying, my coldFlow=2 assignment would immediately get reflected back to the loop 1 instance, and it would always be true that loop1.heatExchanger and loop2.heatExchanger would always have the exact same fields set to the exact same values.
I have considered making heatExchanger not contained within either loop (i.e. a separate variable in the same scope as loop1 and loop2) so there would be only a single instance of that heat exchanger, but that then loses the fact that that heat exchanger is indeed part of loop 1 and also is part of loop 2. Looking at the contents of the loop1 variable for example I wouldn't have any information about that important piece of the loop.
I do create the heatExchanger structure and all the fields it will have at the beginning of my code and initialize the fields with NaNs to make it obvious when things haven't been set/copied. That does help identify mistakes of forgotten copies, but it doesn't at all address the need for those copies in the first place or help when a copy is missed after a value has been set initially.
Any help/guidance on a good way to approach this or structure my variables/code is greatly appreciated.