Conversting a Matlab M- file to c

N

Thread Starter

NA

Hello

i am new user of Matlab and c. but i am taking a project of control system toolbox and i need to convert M-file to c and i don't know how it can be. Please would you let me know is there any converter or any other method to convert it?<pre>
This is my m-file:-

function [Phi, Gamma] = c2d(a, b, t)
%C2D Conversion of continuous-time models to discrete time.
%
% SYSD = C2D(SYSC,Ts,METHOD) converts the continuous-time LTI
% model SYSC to a discrete-time model SYSD with sample time Ts.
% The string METHOD selects the discretization method among the
% following:
% 'zoh' Zero-order hold on the inputs
% 'foh' Linear interpolation of inputs (triangle appx.)
% 'imp' Impulse-invariant discretization
% 'tustin' Bilinear (Tustin) approximation
% 'prewarp' Tustin approximation with frequency prewarping.
% The critical frequency Wc (in rad/sec) is specified
% as fourth input by
% SYSD = C2D(SYSC,Ts,'prewarp',Wc)
% 'matched' Matched pole-zero method (for SISO systems only).
% The default is 'zoh' when METHOD is omitted.
%
% For state-space models without delays,
% [SYSD,G] = C2D(SYSC,Ts,METHOD)
% also returns the matrix G mapping the states xc(t) of SYSC to
% the states xd[k] of SYSD:
% xd[k] = G * [xc(k*Ts) ; u[k]]
% Given some initial condition x0 for SYSC, an equivalent initial
% condition for SYSD is
% xd[0] = G * [x0;u0]
% where u0 is the initial input value.
%
% See also D2C, D2D, LTIMODELS.

%Other syntax
%C2D Conversion of state space models from continuous to discrete time.
% [Phi, Gamma] = C2D(A,B,T) converts the continuous-time system:
% .
% x = Ax + Bu
%
% to the discrete-time state-space system:
%
% x[n+1] = Phi * x[n] + Gamma * u[n]
%
% assuming a zero-order hold on the inputs and sample time T.
%
% See also D2C.

% J.N. Little 4-21-85
% Copyright 1986-2007 The MathWorks, Inc.
% $Revision: 1.1.8.3 $ $Date: 2007/11/09 19:48:35 $

error(nargchk(3,3,nargin));
error(abcdchk(a,b));

[m,n] = size(a);
[m,nb] = size(b);
s = expm([[a b]*t; zeros(nb,n+nb)]);
Phi = s(1:n,1:n);
Gamma = s(1:n,n+1:n+nb);

% end c2d</pre>
 
NA,

If you want to convert MATLAB code to C and use built-in MATLAB functions like 'expm(...)', I think your only option is to use a code generation tool like MathWorks' own "MATLAB Coder":

http://www.mathworks.co.uk/products/matlab-coder/

There may be other tools such as dSpace's TargetLink (I have no experience with these):

http://www.dspace.com/en/ltd/home/products/sw/pcgs/targetli.cfm

Neither of these two tools is cheap; please let us all know if you do come across a free or cheaper tool.

Generation of C code from LabView instead of MATLAB may be easier; have you considered using LabView as your design/development tool?

Regards,
Gerrit.
 
Top