matlabFunction: Index exceeds the number of array elements for Matlab R2021a
Show older comments
Hi all,
I am currently trying to write a symbolic expression into a matlab function using the matlab command. This works perfectly for smaller symbolic expressions, but for larger ones I encounter the following error in R2021a:
matlabFunction(C, 'vars', {q, qdot, alpha, l, rho}, 'file', strcat(dpath,'/C_fun'), 'Optimize', true);
Index exceeds the number of array elements (54).
Error in sym/matlabFunction>addVars (line 817)
varArr = addElem(varArr,split(join(elem(start:ende),','),','));
Error in sym/matlabFunction>handleBlocks (line 928)
blocks(end).lhs = addVars(blocks(end).lhs, lhsVars, linesWritten+1,linesWritten+newLines);
Error in sym/matlabFunction>writeOutput (line 616)
blocks = handleBlocks(blocks, body, fmt, lhsVars, rhsVars, ifNargout);
Error in sym/matlabFunction>writeMATLAB (line 506)
blocks = writeOutput(outputs,f,1,sparseMat,blocks,false);
Error in sym/matlabFunction (line 196)
[g, blocks] = writeMATLAB(funs,file,varnames,outputs,body, opts.Optimize, opts.Sparse,
opts.Comments, blocks);
Is there a possibility to increase the maximal length of the matlabFunction command?
Accepted Answer
More Answers (1)
Walter Roberson
on 13 Apr 2021
0 votes
matlabFunction(C, 'vars', {q, qdot, alpha, l, rho}, 'file', strcat(dpath,'/C_fun'), 'Optimize', true);
Index exceeds the number of array elements (54).
You did not mention your release. I predict that you are using R2020a or before (and I am not positive it was fixed in R2020b though I was told i was). If so then you need to use 'Optimize', false .
Unfortunately in a range of versions of MATLAB, matlabFunction with 'optimize' turned on generates incorrect output when it produces output at all. It generates correct output to a point, and then gets messed up. Effects varied: sometimes it generated additional lines but did not use any of them, returning a partial result instead; sometimes (more rarely) it did not generate additional lines but tried to use the elements it would have generated, generating index out of range errors.
I have use matlabFunction to generate fairly large expressions. It can turn painfully slow (especially with optimize turned on.) A small number of releases ago, I think they mentioned that performance had been improved a fair bit, but I am not certain about that.
Note: When you use 'Optimize', 'on', then the matching process seems to be at least O(n^2.3) in my tests. Because of that, if you have an expression made up of adding or multiplying multiple parts, it can be much faster to generate functions for each of the parts individually, and then add an extra function to hook the parts together. Doing this can lose out on some optimizations, but it can cut down a lot when working with expressions that do not have much in common (because the optimizer does not know they do not have a lot in common.)
4 Comments
Maximilian Stölzle
on 13 Apr 2021
Walter Roberson
on 13 Apr 2021
Odd....
I don't think I have tried a long expression with R2021a yet. I did generate some quite long expressions with some earlier versions.
Walter Roberson
on 15 Apr 2021
It turns out that R20201a substantially rewrote file handling code for matlabFunction() .
I was able to identify a bug in the handling and posted the solution into https://www.mathworks.com/matlabcentral/answers/801416-problem-with-matlabfunction-throwing-error-in-latest-matlab-version?s_tid=srchtitle .
However, that bug was in a step after the problem you are observing.
If you post enough of your code for me to test, I will attempt to debug the problem.
Maximilian Stölzle
on 15 Apr 2021
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!