advertisement
from the ECE department...
Conversting a Matlab M- file to c
Engineering and workplace issues. topic
Posted by NA on 23 June, 2012 - 1:25 pm
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?

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


Posted by Gerrit on 27 June, 2012 - 4:37 am
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.

Your use of this site is subject to the terms and conditions set forth under Legal Notices and the Privacy Policy. Please read those terms and conditions carefully. Subject to the rights expressly reserved to others under Legal Notices, the content of this site and the compilation thereof is © 1999-2013 Nerds in Control, LLC. All rights reserved.

Users of this site are benefiting from open source technologies, including PHP, MySQL and Apache. Be happy.


Fortune
Man is the only animal that blushes -- or needs to.
-- Mark Twain
Advertise here
Advertisement
our advertisers
Help keep our servers running...
Patronize our advertisers!
Visit our Post Archive