How can I convert matlab code to C?

4 views (last 30 days)
Yousef
Yousef on 10 Dec 2014
Answered: pramod D Y on 16 May 2020
I want to convert my matlab code to c. In the video posted online regarding this, says I should type coder in the command window to build a 'prj' file first. But when I type it I get this error: Undefined function or variable 'coder'. By the way, I already checked to see whether there is supported compiler or not by typing 'mex -setup' in the command window. I would appreciate if anybody could help me out?

Answers (2)

Orion
Orion on 10 Dec 2014
You need the toolbox Matlab Coder.
in Matlab, type
ver
if Matlab coder is not part of the result, then you can't generate C code from M code.
You're gonna need to buy this toolbox if you want to use coder.
  3 Comments
Adam
Adam on 10 Dec 2014
I don't know what kind of license you have, but yes it is quite expensive since it creates fast C code out of usually slower Matlab code, including support for many builtin Matlab functions.
We have been considering adding it to our Matlab licenses, but the cost means it needs to be very carefully evaluated as to its usefulness.
Sean de Wolski
Sean de Wolski on 10 Dec 2014
Though how expensive is it to rewrite MATLAB Code in C by hand!?

Sign in to comment.


pramod D Y
pramod D Y on 16 May 2020
%%
% P = Load vector starting from the right most member
% A = Area vector starting from the right most member
% E = Modulus of Elasticity vector starting from the right most member
% L = Length vector starting from the right most member
%% EXAMPLE:
P=[ 0 -60 0 -40]*1000;
A=(pi/4)[40.^2 40.^2 30.^2 30.^2](10^(-6));
L=[180 120 100 100]./1000;
E=[ 200 200 105 105 ]*10^9;
%% Compute the total force for the ith member
I=length(L); % Numebr of elements
Fi=0;
for i=1:I
Fi=Fi+P(i);
Pf(i)=Fi; % Pf=Computing the total force in each element
end
%% Compute the stress in each element
Sf=Pf./A; % Sf=Computing the stress in each element
%% Compute the deformation in each element
Df=Sf.*L./E; % Df=Computing the deformation in each element
%% Compute the total deformation
DT=sum(Df); % DT = computing the total deformation
%% Compute the deformation due to unit load
UD=L./(A.*E);
%% Compute the total deformation due to unit load
UDT=sum(UD);
%% Reaction at B
RA=-DT./(UDT);
%% Reaction at A
RB=sum(P)+RA;
%% Final stress in each element
Sigma=Sf+RA./A;
%% Final deformation in each element
Delta=Df+RA.*UD;
%% Final Results
II=1:I;
%% Display values
disp(['Reaction at A : ' num2str(RA./1000) ' kN'])
disp(['Reaction at B : ' num2str(RB./1000) ' kN'])
ALL=[II' (1/1000000)*Sigma' 1000.*Delta'];
disp([' Element Stress (MPa) Deformation (mm)' ])
disp(['----------------------------------------------------------' ])
disp(ALL)
disp(['----------------------------------------------------------' ])

Categories

Find more on Stress and Strain in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!