Monthly Archives: October 2015

Compiling a Matlab C code with GCC/G++

In this post, I am sharing how to compile a C code generated by Matlab C coder with GCC compiler. Let me explain with an example.

1. Generating a C code with Matlab C coder.

I will use a very simple Matlab code, and the below is the code

function y = add_coder(a,b)

With the Matlab C coder, let’s auto-generate the C code. You have to specify the types of variables and some other options. If you just read the dialog box, it might not be difficult.

As a result, you can see this kind of result. If your Matlab version is not 2015, it might be little different, but its big flow is almost same.



2. Let’s compile the code generated by Matlab C coder

This is my main.cpp file that is modified from the auto-generated main.cpp by Matlab C coder

#include "rt_nonfinite.h"
#include "add_coder.h"
#include "add_coder_terminate.h"
#include "add_coder_initialize.h"
#include "stdio.h"

int main()
  /* Initialize the application.
     You do not need to do this more than one time. */

  /* Invoke the entry-point functions.
     You can call entry-point functions multiple times. */
  double a=10;
  double b=20.0;
  double y;
  y = add_coder(a, b);
  printf("y: %lf\n",y);

  /* Terminate the application.
     You do not need to do this more than one time. */
  return 0;

I located this main.cpp file with other all codes.

Now let’s compile all codes. Just compile main.cpp with all other cpp files. like this.



I am attaching all related files as a zip file. <Download>


Isn’t it easy??!!!

Have a good day~~!

Vertex angle from three points, Matlab code

I am sharing a Matlab code to get a vertex angle from three points.

This is the code, and you can download this one from here <Download>


function [ang] = get_vertex_ang_from_three_points(p0,p1,p2)

% get the vertex angle at p0 from three points p0,p1,p2
% p0, p1,p2 are two dim vector
%%% example
% p0=[0;0];p1=[1;0];p2=[0;1];
% get_vertex_ang_from_three_points(p0,p1,p2)
% answer = 1.57 (45 deg)

v1 = p1-p0;
v2 = p2-p0;
ang = acos(v1′v2/(norm(v1)norm(v2)));

It is very easy to use. you can get the vertex angle from three points, It is written in Matlab code

Good luck~~