What does this error message mean - No class precondition specified for input 'x' of function 'square'.

3 次查看(过去 30 天)
The function I was using is as follows:
function y = square(x)
%
% calculate the square of the given number 'x'
%
% Arguments:
%
% x (input) value to be squared
%
% y (output) the result of the square
%
y = x*x;
% end of square function
  3 个评论
Valline
Valline 2011-10-25
I am trying to generate a mex function for the m-function given above, using matlab coder. When I type this line of code into the command window, this is when the error message appears:
'codegen square'
Walter Roberson
Walter Roberson 2011-10-26
Then I would say that my answer below is definitely the root cause of your problem. Unfortunately I do not know enough about Coder to tell you what you need to fix.
Well, other than the _first_ thing you need to fix is that you need to define for yourself (and document) what datatype you want this code to operate upon.

请先登录,再进行评论。

回答(3 个)

Alexander Bottema
Alexander Bottema 2011-10-26
The error is given because you're trying to generate C code (which we later will automatically compile to a MEX function) for a function where you haven't specified any information about the function inputs.
There are several ways you can specify additional knowledge about the function inputs:
1. Use -args when compiling with 'codegen'. For example,
codegen square -args {zeros(3,3)}
Will generate a square_mex.<mexext> for which you can square any 3x3 matrix of class double.
codegen square -args {coder.typeof(0, [3 3], [1 1])}
Will generate a square_mex.<mexext> for which you can square any matrix with sizes up to 3x3 of class double.
codegen square -args {0}
Will generate a square_mex.<mexext> for which you can square a scalar only of class double.
codegen square -args {coder.typeof(complex(0), [1 100], [0 1])}
Will generate a square_mex.<mexext> for which you can square a matrix of complex doubles where the first dimension is fixed, and the second dimension is variable with an upper bound of 100 elements.
Check for more information by doing 'help codegen' We generate different code for all the above scenarios because we take advantage of all static type information so we can generate as efficient C code as possible.
2. Instead of using -args you can use assertions on the function inputs that will equal the same kind of information:
function y = square(x) %#codegen
assert(isa(x, 'double'));
assert(all(size(x) <= [10 10]));
y = x*x;
Here you can square any matrix up to 10x10 (real and of class double).
Prior to MATLAB Coder, i.e. Embedded MATLAB and the emlc/emlmex commands, we assumed scalar doubles if assertions/args were not given. However, this confused users when they try to call the generated MEX function with other input sizes.
I hope this helps.
Thanks, Alexander
  2 个评论
YOGESH
YOGESH 2013-12-6
编辑:YOGESH 2013-12-6
I have installed the matlab coder toolbox on Linux 64 machine on compute node in silent mode.
When I try to generate the c code by using the codegen, I always get an error. The error is pasted here
>> codegen -c magicsquare -args {coder.typeof(0)}
??? Argument must be a constant. MAGIC does not support variable-size output.
Error in ==> magicsquare Line: 10 Column: 5 Code generation failed: To view the report, open('/users/home/****/****/****/test_coder/codegen/mex/magicsquare/html/index.html'). Error using codegen
m = magic(n) is the line: 10 in magicsquare.m
This happens for even the hello_word.m program.
Is there any other options I should add to solve it.
Thanks, Yogesh

请先登录,再进行评论。


Daniel Shub
Daniel Shub 2011-10-25
What do you get with
which square -all
square is a function in the signal processing toolbox. Do you have any other functions or variables named square.

Walter Roberson
Walter Roberson 2011-10-25
I do not know the solution, but I believe I recognize the problem.
Because your routine is so general, MATLAB is not able to determine what kind of data you will be feeding in to it. For example, will the data be double precision numbers? int32? A matrix that needs to be matrix-multiplied by itself? Or perhaps the input might even be a member of a class that defines its own mtimes() operator that does something pretty different than multiplication.
In order to get your routine to compile, you need to nail it down to one particular datatype.
Unfortunately I do not know at the moment how one does that.
  2 个评论
Walter Roberson
Walter Roberson 2011-10-25
I do not have the compiler. The poster tagged with "mex", but I suspect they are talking about the MATLAB Compiler or possibly about Embedded MATLAB, as those would want to know what data-type they were dealing with.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Input Specification 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by