Please use Matlab's tools for an automatic indentation. Then the different levels of the IF conditions get clear immediatly.
You can be sure that the IF conditions are calculated accurately even inside a loop. This means, that the problem must be somewhere else. E.g.:
Demand(k,1)=Demand;
SOC(k,1)=SOC;
Export(k,1)=Export;
This cannot run. As soon as e.g. Demand is a vector, you try to assign the complete vector to one of its elements. This must cause an error. If you do not get an error, you are running a different code.
Please post the code you are actually running. Perhaps you have stored multiple versions of the M-file and Matlab does not run the file you are expecting. This can be found out by the debugger easily: Set a breakpoint in the code and step through it line by line.
Note: If you have checked "if NetLoad >= 0" already, there is no need to check "elseif NetLoad < 0" again. A simple "else" is enough. At least, if the value of NetLoad is not NaN, but if this should be tested, better use isnan directly.
Best Answer