MATLAB: Saving/Reading variables based on ga optimization results

gaMATLABoutput

I have a question with regards to the ga solver in which I want to get the output of specific intermediate variables within the problem I am trying to solve. I only want to see the results corresponding to the optimal values.
In my objective function, I am trying to minimize the electricity cost of a building by optimizing the resistance of the insulation materials of the walls. The optimization works fine and I can get the x values and the cost of electricity by default. The script is long but lets say I have the zone indoor temperature (ZoneTemp) and the electricity end use (ElecUse) as intermediate variables that the final electricity cost depends on, how can I get these 2 variables as an output from the ga solver? I am sure there is a way to store these values to be read after the optimization is done. I tried to look at the "Custom Output Function for Genetic Algorithm" documentation but I am somehow lost as I am new to MATLAB.
I will just give a simple example of what I am trying to get
%This is my objective function in which I am using ga to minimize the toal cost of electricity
%To calculate the TotalCost there are too many variables and equations, these include ElecUse and ZoneTemp.
TotalCost = ElecUse * ElecRate
ElecUse = LightLoad + HVACLoad + PplLoad … etc…
HVACLoad = ZoneTemp*…… etc…
I hope this is clear…
Any hints or examples would be appreciated. Thank you!

Best Answer

See my code here: https://www.mathworks.com/matlabcentral/answers/514594-plot-the-function-value-according-to-the-value-of-the-optimized-parameter-by-genetic-algorithm to see how to extract the best values of x for all iterations. The once extracted, you can use those to the values of x to calculate ZoneTemp and ElecUse according to their formula.