Converting Matlab code to C code
显示 更早的评论
I'm trying to convert the following matlab code to c code, I did it both in windows and in linux systems by using Visual Studio in windows and gcc in linux as the compiler. However I couldn't get correct results by running the generated c code. Can anyone have a idea how to modify this matlab code in order to generate the matched c code? I would appreciate that!
function matc(n)
val = 0;
for i = 1:n
x = 2*rand(3,1);
val = val + f(x);
end
val = 2^3*val/n
6 个评论
Friedrich
2012-2-29
What is f ?
AW
2012-2-29
James Tursa
2012-2-29
Please format your code so it is readable. Is the function supposed to return val? Are you looking for a mex function, or just a C-language code snippet?
AW
2012-2-29
Kaustubha Govind
2012-2-29
How exactly do the results vary?
AW
2012-2-29
回答(6 个)
Kaustubha Govind
2012-3-1
To expand's on Walter's answer, your MATLAB code should look like this:
function val = matc(n)
val = 0;
for i = 1:n
x = 2*rand(3,1);
val = val + f(x);
end
val = 2^3*val/n
function y = f(x)
y = sin(x(1) + x(2)^2 +x(3)^3);
(Note how 'val' is being returned from the function by adding it to the "function" line).
And your C code should look like this:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "matc.h"
int main()
{
printf("matc=%g\n", matc(0.2));
return 0;
}
Note how an input value needs to be passed into matc.
Walter Roberson
2012-2-29
0 个投票
You are not passing any value from the C code to the matc() routine. The initial value of an undefined parameter is undefined, so any result could occur.
AW
2012-2-29
0 个投票
1 个评论
Walter Roberson
2012-2-29
Yes, having it declared as type double is fine, but you still need to pass a specific variable in to matc() when you make the call to matc()
printf("matc=%g\n", matc(42.));
AW
2012-3-1
0 个投票
10 个评论
Walter Roberson
2012-3-1
What does your code look like at present?
AW
2012-3-1
Walter Roberson
2012-3-1
Looks plausible. I would put a line in matc() displaying the value of n at the beginning, to cross-check that you are getting the input you expect.
AW
2012-3-2
Kaustubha Govind
2012-3-2
Try putting a statement "disp(val)" at the bottom of your "matc" function and recompile the code. Does the MATLAB code display a zero too?
AW
2012-3-2
Walter Roberson
2012-3-2
So somehow the argument isn't getting passed in correctly, I don't know why at the moment.
Say, what does matc.h contain?
AW
2012-3-2
Walter Roberson
2012-3-2
Hmmm, I wonder if real_T is single or double precision?
And I wonder if the code generator is treating 1000 as integer?
Could I ask you to indulge my curiosity and try 1000. (with the perod to force syntactic floating point) ? And to also try with
single(1000) and double(1000) ?
AW
2012-3-2
AW
2012-3-7
0 个投票
1 个评论
Kaustubha Govind
2012-3-8
AW: I would recommend using a "disp" command in your script. Since disp is not supported for code-generation, you need to add this line at the top of your function:
coder.extrinsic('disp');
%eml.extrinsic('disp') for older versions of MATLAB
Then, generate a MEX-file from your code (instead of standalone C code). You can use the command:
>> codegen -config:mex matc.m
Then run the generated MEX-file in MATLAB. What does the disp command show?
AW
2012-3-21
2 个评论
Kaustubha Govind
2012-3-22
Thanks for posting back your answer, AW. So what exactly was the error you were making before?
Walter Roberson
2012-3-22
The difference seems to be the %#codegen
类别
在 帮助中心 和 File Exchange 中查找有关 MATLAB Coder 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!