Matlab Coder and Ceval

I'm trying to use matlab coder and would like to display some text on the terminal from which I run the compiled C code. I'm having issues with the simple printf
The code in matlab is:
for n=2:10
coder.ceval('printf','%d',n);
end
where n is the thing I want to print out to teh screen
The code I end up getting is:
static const char_T cv0[2] = { '%', 'd' };
char_T cv1[2];
n=2;
while (n <= 10)
for (i0 = 0; i0 < 2; i0++) {
cv1[i0]=cv0[i0];
}
printf(cv1, (real_T)n);
which does not seem to output the value of n, but instead just 0s
The code I WANT is:
printf("%d",n);
which does what I want when I put it in the .c file myself and compile
Any help would be appreciated

 Accepted Answer

Try:
for n=2:10
coder.ceval('printf','%d ',int32(n));
end
or
for n=2:10
coder.ceval('printf','%f ', n);
end
In MATLAB, 'n' is double by default.
EDIT:
To pass in a newline character, you can use the ASCII value (=10 for \n):
formatString = ['%d' char(10)];
for n=2:10
coder.ceval('printf', formatString, int32(n));
end

4 Comments

CP
CP on 24 Aug 2011
Thanks, that seems to work. Interesting, why is it that when I do printf("%d",n) manually that it then works? Also, how would I add a \n to that by the way? It seems to just print it out when I do instead of making a new line.
If the code indicated by CP is a copy and paste of the generated code, then the generated code is an infinite loop and will not produce any output at all. In C, the syntax of "while" is
while (condition) statement
The "(condition)" part is fine in the code we are shown.
The "statement" in the code we are shown would be the "for" statement, the extent of which ends at the "}" -- i.e.,
for (i0=0; i0 < 2; i0++) { cv1[i0]=cv0[i0]; }
This entire statement is what is to be repeated in the C "while", and nothing else is to be repeated by that "while". Each individual execution of the "for" statement is fine by itself, but there is nothing in that extent that modifies the variable "n", so the while is going to infinite loop.
If the original poster got any output from this code then their compiler is broken.
It is plausible that the code we are shown is not the actual code that was generated. If that is the case, then we need to see the actual code in order to determine whether correct (if inefficient) code was generated.
CP
CP on 24 Aug 2011
It's not copy pasted, some brackets and unimportant things are missing. there are indeed brackets around the while statements. The while loop has a ton of statements, one of which is n++, both the for loop and the printf statement are also within the while loop.
@CP: When you say you do printf("%d",n) manually, do you mean you've written C code to do this? If yes, you are probably declaring 'n' as an int. MATLAB Coder generates 'n' as a real_T (double).
I edited my answer to show how to insert a newline character.

Sign in to comment.

More Answers (1)

There's a serious flaw in the example given. C uses NUL character for string termination, therefore you need to do:
formatString = ['%d' 10 0];
coder.ceval('printf', formatString, int32(n));
Otherwise you can get unexpected crashes.

Community Treasure Hunt

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

Start Hunting!