Ahmad Shah
Ahmad Shah

Reputation: 321

How to use C code generated by Matlab?

I used the Matlab Coder to produce C code for a simple Matlab Array adding function which adds the elements of two arrays. Once done, the Matlab Coder gives me a package containing .c and header files ( which also includes a C file of the function itself ).

  1. How do I use these C files in for e.g. programs like Dev C++ or Code::Blocks ?

  2. How do I initialize emxArray_real_T variables to incorporate elements of an integer array ?

Upvotes: 4

Views: 1533

Answers (1)

Gabriel Ilharco
Gabriel Ilharco

Reputation: 1679

If you have an IDE that supports c, like Dev C++ or Code Blocks, you just need to open the file with that IDE, compile and run it. For Code Blocks, press F9 to compile and run your code.

Edit: The undefined reference to emxInitArray_real_T error is due to an linking error. You can make your costumized MakeFile and select it in (for CodeBlocks: Project->properties->Project setting). Try this one:

CC=g++
CFLAGS=  -g
OBJECTS= main.o
LIBS = -Llibs -lMat

# --- targets
all:    main
main:   $(OBJECTS)
        $(CC)  -o main $(OBJECTS) $(LIBS)

main.o: main.cpp
        $(CC) $(CFLAGS) -Ilibs -c main.cpp

Edit 2: For Dev C++:

1 - Create a new project using File >> New Project. You can ignore the C/C++ options if you use a custom makefile. Also, an empty project will do.

2 - Add the source and header files to the new project using Project >> Add to Project or the '+' sign in the middle of the top toolbar.

3 - Go to Project >> Project Options (Alt+P) >> Makefile and tick the 'Use custom makefile' option. Then point Dev-C++ to the custom makefile below.

, as pointed in this post.

Upvotes: 2

Related Questions