Matlab coder Non-constant expression or empty matrix
5 views (last 30 days)
Show older comments
Hello, I am trying to make a function made by Matlab transformed into MEX file to enhance execution speed of the function but the coder show the error:
Non-constant expression or empty matrix. This expression must be constant because its value determines the size or class of some expression. The part of the code that causes the problem is
indx=mat==max(mat);
nominees={acceptedB{indx,:}};
I also tried not to use logical indexing using
indx=find(mat==max(mat));
but the same error appeared. Any idea how to solve this problem without affecting execution speed of the function ?
0 Comments
Answers (2)
Walter Roberson
on 5 Jun 2016
First assign to the variable an array that is of the right class and is the maximum size that could possibly be encountered. For example,
indx = zeros(numel(mat),1);
You need to use numel of mat because of the possibility that all of the elements in mat are the same value so your find() might return every index.
Then you can do
indx = find(mat(:) == max(mat(:)));
The result might well be shorter than the size you originally allocated, and that is okay: you can reassign a matrix as shorter but not as longer.
However, I am concerned because it looks to me as if
nominees={acceptedB{indx,:}}
is indefinite size. Would
nominees = acceptedB(index,:);
be acceptable instead?
7 Comments
Ayim Manuel De la fuente de Pablo
on 5 Mar 2018
Hi,
I am facing this same problem, did you get to solve it?
Thanks!
Chris Volpe
on 28 Aug 2023
I am having the same error, but in the context of referencing a struct field whose name is contained in a variable, i.e. an experssion of the form "foo.(bar)". However, for the present context, the proposed solution seems to suggest using non-curly braces to index into a cell array, which Coder considers gauche.
Pasquale
on 5 Nov 2024
Edited: Pasquale
on 5 Nov 2024
In my case I had the same error using a Matlab Function. The code inside preallocated the output matrix to force the right sw type (single) and size (pred_hor x ny), with pred_hor and ny being workspace parameters:
Ref = single(zeros(pred_hor,ny));
I got that error on the above line, and I solved it by going to data editor ("Edit Data") for the matlab function, and specifying that the two parameters are not tunable (I had to deselect the corresponding chck box). This allowed the parser to infer the right dimension and run correctly.
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!