Niel: Don't mean to discourage you, but please post your comments on this thread instead of sending personal messages in reply (we highly discourage contacting contributors personally on this forum since our participation is voluntary and not part of our job responsibilities. Thanks or understanding!)
---Message from Niel follows---
Thank you for pointing out that the errors I received were "compile time" errors. By pressing Cntl+D, I indeed received the same errors. The reason I thought that the problem lies with Simulink is that the same code ran perfectly in Matlab's normal code format. I did make a few changes to the code in order to incorporate it into a Matlab function block. Why would the same piece of code suddenly given errors in Simulink?
--------------------------------
When your code runs in the MATLAB Function block, it actually generates C code from the MATLAB code and generates a MEX-file from it. It is this MEX-file that is used for execution of the block. The compilation process for generating the C code is a lot stricter than how the MATLAB interpeter works, which is why you're seeing the failure only when using the code in the block. You should however see the same compilation error if you attempt to generate code from the function using MATLAB Coder (outside of Simulink).
Now, coming back to the original question - it looks like your code is too long to read legibly even if you paste it here completely. Are you absolutely sure that the variable that the error complains about is assigned outside of the "if" section as well? To be safe, you could try assigning it to a value at the top of your function, and then let it get reassigned later.
Best Answer