Matlab Coder Segmentation fault

8 次查看(过去 30 天)
Hi, I'm trying to implement a test where I create an executable from a Matlab function where the executable takes in a variable number of command line arguments and then sends them into the Matlab function. Currently the Matlab function is trivial (just returns the mean), however I'm having trouble writing my main.c function.
I've gotten the code working where I see that I'm correctly getting the data into the c-code (If I use the display command I can see the inputs.), however when I try to get that data into an array and then the array into the Matlab function I get a segmentation fault. I think that I'm messing up something in terms of how I'm creating the array, or how I'm sending the array into the Matlab function. I tried using 'emxArray_real_T', as per the run_atoms example, but wasn't able to get that to work. My knowledge of C is limited, and while I've put a lot of work into this, I haven't been able to get this to work.
The code I've been using is below; if someone is able to help me out with this it would be greatly appreciated!
Thanks, David
/*** test3_main.c *************************************************/
#include <stdio.h>
#include "codegen/exe/test3/test3.h"
#include "codegen/exe/test3/test3_emxAPI.h"
int main(int argc, char **argv)
{
int size_array = argc - 1;
int index;
double array_vals[size_array];
for( int i = 1; i < argc; ++i ) {
index = i - 1;
array_vals[index] = atof(argv[i]);
}
double *result;
test3(array_vals,result);
printf("%f \n",result);
}
/************************************************************/
/*** test3.m *************************************************/
function result = test3(data, result) %#codegen
result = mean(data);
/************************************************************/
cfg = coder.config('exe');
cfg.DynamicMemoryAllocation = 'AllVariableSizeArrays';
codegen test3 test3_main.c -args {coder.typeof(0,[1 inf],1), double(10)} -config cfg
./test3 4 5 6 7
Segmentation fault

采纳的回答

Arnab De
Arnab De 2013-3-16
Your C code, in its present form, will not compile. I suspect that the executable you have is a stale result of compilation of an incomplete earlier version of the code. Nevertheless, here is how you can make this code working with minimal changes:
#include <stdio.h>
#include "codegen/exe/test3/test3.h"
#include "codegen/exe/test3/test3_emxAPI.h"
#include "codegen/exe/test3/test3_emxutil.h"
int main(int argc, char **argv)
{
/* Ansi C requires you to declare your variables
in the beginning of the function */
int size_array;
int index;
double *array_vals;
double result;
int i;
emxArray_real_T *input;
size_array = argc - 1;
/* You need to do a dynamic memory allocation here
because size_array is not a constant */
array_vals; = malloc(size_array * sizeof(double));
for(i = 1; i < argc; ++i ) {
index = i - 1;
array_vals[index] = atof(argv[i]);
}
/* Create the emxArray to pass to test3. See test3_emxAPI.h */
input = emxCreateWrapper_real_T(array_vals, 1, size_array);
/* Note the function signature of test3 in test3.h */
test3(input, &result);
printf("%f \n",result);
/* Free the dynamically allocated memory and emxArray */
emxFree_real_T(&input);
free(array_vals);
}
  1 个评论
David
David 2013-3-18
That is fantastic! Thank you very much for your help, your solution worked without any problems. My knowledge of C is (quite obviously) rather limited, so you've really saved the day. Thank you, David

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Coder 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by