Problem with matlabFunction throwing error in latest MATLAB version

15 views (last 30 days)
I was digging into some code I wrote a few years ago that runs fine on MATLAB 2016a and it's throwing an error at a line of code that uses matlabFunction to create a function file from some symbolic expressions and variables. The file to be created is an objective function file with gradient for a nonlinear optimization.
matlabFunction(Cost,gradCost,'file',filename,'vars',ObjFunVar);
Here Cost and gradCost are are the objective function and gradient which are 1x1 sym and 360x1 sym respectively. ObjFunVar is a cell array that contains the symbolic variables for the optimization and symbolic variables through which I'm passing parameters to the function.
ObjFunVar = {x,T__out,p__E,Delta__t,T__in0,eta__fan,eta__heat,G__0,P__fan,xi__mix,Scale__f,Scale__h};
The variables for the optimization are in x, a 360x1 sym. T__out and p__e are 120x1 sym, and the rest are 1x1 sym.
The error I'm getting is this
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in sym/matlabFunction>computeVarnames (line 902)
allNames = matlab.lang.makeUniqueStrings([repelem("ft",1,nuOfBlocks-1), "ft"
allVars],1:nuOfBlocks-1,namelengthmax);
Error in sym/matlabFunction>postProcessing (line 826)
[newFunctionNames, newArgumentName] = computeVarnames(blocks);
Error in sym/matlabFunction (line 198)
blocks = postProcessing(blocks);
Error in MPC_Heat_Only3 (line 160)
matlabFunction(Cost,gradCost,'file',filename,'vars',ObjFunVar);
As I said, this doesn't happen if I run the code on MATLAB 2016a - everything executes fine. I've found that if I scale back the size of the optimization so that x and gradCost wind up being 150x1, T__out and p__e are 50x1, then there is no error and the whole thing executes without a problem.
Is there something about matlabFunction, the Symbolic Toolbox, or something else that has been changed that could be causing this?
EDIT: I've uploaded the two files of code. MPC_Heat_Only3.m is the code to run. HVAC_MPC_Controller_parameters_Heat_Only_3.m initializes a bunch of parameters and needs to be in the same folder as MPC_Heat_Only3.m.
  6 Comments
Walter Roberson
Walter Roberson on 15 Apr 2021
I identified a bug and tested a work-around.
After the work-around, you have a problem in line 288 of your code, that you want to save the non-existent variable Tdiff.
Anthony Mogis
Anthony Mogis on 15 Apr 2021
Thanks so much for looking into this! There's no way I could have found that bug myself.
And thanks for the heads up about Tdiff. I cleared out some old unused code and must have missed that variable.

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 15 Apr 2021
There is a bug in matlabFunction in R2021a, in a section of code that did not exist at all before (and had no analog at all before -- a completely new bug.)
To fix the problem you will need to edit your matlabFunction file.
Go to line 898 and replace
allVars = cell.empty;
with
allVars = cell(1,0);
I will report this to Mathworks in a second.
  3 Comments
Walter Roberson
Walter Roberson on 15 Apr 2021
matlabFunction is normally stored in @sym and so is considered part of the class implementation of sym class, and so has access to sym internals that your revised function does not have (because your revised code is not stored in @sym )

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!