Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Converting Matlab .m file into C code

Subject: Converting Matlab .m file into C code

From: Ashraf Awad

Date: 8 Nov, 2007 04:53:00

Message: 1 of 17

Is there a chance, in Matlab, to convert ".m file" into
a "c code", to run it separately from Matlab?. How, if it
is possible?
Thank you
Ashraf awad
 

Subject: Converting Matlab .m file into C code

From: Becky J.

Date: 2 Oct, 2008 19:49:02

Message: 2 of 17

"Ashraf Awad" <a_awad@hotmail.com> wrote in message <fgu4nb$6fe$1@fred.mathworks.com>...
> Is there a chance, in Matlab, to convert ".m file" into
> a "c code", to run it separately from Matlab?. How, if it
> is possible?
> Thank you
> Ashraf awad
>


Did you find an answer to this question?

Subject: Converting Matlab .m file into C code

From: Walter Roberson

Date: 2 Oct, 2008 21:54:15

Message: 3 of 17

Becky J. wrote:
> "Ashraf Awad" <a_awad@hotmail.com> wrote in message <fgu4nb$6fe$1@fred.mathworks.com>...
>> Is there a chance, in Matlab, to convert ".m file" into
>> a "c code", to run it separately from Matlab?. How, if it
>> is possible?
 
> Did you find an answer to this question?

It is not possible except with older (unsupported) versions of Matlab with
the older (unsupported) matlab compiler. Current (supported) Matlab versions
compile to threaded interpreted code, not to C or C++.

I should expand a bit and mention that a couple of Matlab tools such as the
Real Time Workshop, and Embedded Matlab, *do* generate C, for the functions
they support.

Subject: Converting Matlab .m file into C code

From: Judy

Date: 2 Oct, 2008 23:11:02

Message: 4 of 17

I thought that the MATLAB Compiler converts the m code into C.. So instead it is the libraries that are being shared and C can read the m code in this way?

In other words, we would still need to use matlab as an interim step as the compiler to get to C..?

Subject: Converting Matlab .m file into C code

From: Walter Roberson

Date: 3 Oct, 2008 00:36:26

Message: 5 of 17

Judy wrote:
> I thought that the MATLAB Compiler converts the m code into C..

Not any more.

> So instead it is the libraries that are being shared and C can read the m code in
> this way?

> In other words, we would still need to use matlab as an interim step as the
> compiler to get to C..?

Unless you are using one of the Builders, or Embedded Matlab, or the Real Time
Workshop, then the modern versions of the matlab compilers transform each .m file
into encrypted binary data files. The matlab compiler "executable" is a program that
knows how to decrypt the binary data files and step through the resulting
structured binary data, each bit of which is interpreted as either control logic
or calls to the matlab libraries. No C is generated or compiled (not unless
you had some mex in the mix.)

With the generated files being binary data files, they are portable to different
operating systems: you just have to replace the interpreter portion and use the same
encrypted binary data files, instead of having to generate code for each different
operating system and each different architecture that you want to run on.

Subject: Converting Matlab .m file into C code

From: Alamzeb

Date: 7 Oct, 2008 21:22:01

Message: 6 of 17

Hey Walter,

In response to your comment:
>It is not possible except with older (unsupported) >versions of Matlab with
>the older (unsupported) matlab compiler. Current
>(supported) Matlab versions
>compile to threaded interpreted code, not to C or C++.

Can I get the older matlab version somehow? and would it generate c code for guide commands as well?

Subject: Converting Matlab .m file into C code

From: Arnaud Miege

Date: 8 Oct, 2008 08:03:05

Message: 7 of 17


"Alamzeb " <alamzeb.khan@nov.com> wrote in message
news:gcgjtp$k1e$1@fred.mathworks.com...
> Hey Walter,
>
> In response to your comment:
>>It is not possible except with older (unsupported) >versions of Matlab
>>with
>>the older (unsupported) matlab compiler. Current
>>(supported) Matlab versions
>>compile to threaded interpreted code, not to C or C++.
>
> Can I get the older matlab version somehow? and would it generate c code
> for guide commands as well?
>

The MATLAB Compiler is used for deployment, i.e. sharing MATLAB
applications with people who do not have MATLAB installed, but not for
generating C code. If you want to generate C code from MATLAB for running in
real-time on an embedded microprocessor, then you need to use Embedded
MATLAB:

http://www.mathworks.com/products/featured/embeddedmatlab/
http://www.mathworks.com/access/helpdesk/help/toolbox/eml/

Note that only a subset of the MATLAB language is supported by Embedded
MATLAB for code generation and that it requires Real-Time Workshop
(http://www.mathworks.com/products/rtw/).

Hope this helps,

Arnaud

Subject: Converting Matlab .m file into C code

From: Walter Roberson

Date: 8 Oct, 2008 16:16:16

Message: 8 of 17

Alamzeb wrote:
 
> In response to your comment:
>> It is not possible except with older (unsupported) >versions of Matlab with
>> the older (unsupported) matlab compiler. Current
>> (supported) Matlab versions
>> compile to threaded interpreted code, not to C or C++.
 
> Can I get the older matlab version somehow? and would it generate c code for guide commands as well?

I think it would be possible to get ahold of the media through some channel or other
(possibly of suspect legality), but I don't think Mathworks would issue a license for you.

The only possible exception I can think of, is if you were to literally buy an entire
company that happened to have a legal license for an appropriate version: then it would
just be a matter of getting a new key for your license. Licenses are not transferable
except (if I recall correctly) when the entire company is purchased.

Recall that if you did get an appropriate old version, then it would not support a number
of the newer features. To the extent that it supported GUIDE, it would do so only to the
limit of the constructs that GUIDE generates that were supported in that old release.

Subject: Converting Matlab .m file into C code

From: Paul Mennen

Date: 9 Oct, 2008 13:53:13

Message: 9 of 17

"Alamzeb " wrote
 
> Can I get the older matlab version somehow? and would
> it generate c code for guide commands as well?

I usually use Matlab ver 6.1 for that reason.
(I like the older compiler far better). I've
compiled many GUIs with it, although I don't use
guide so I can't report directly on that. I'm
pretty sure it would work on guide applications
though, but you would have to use the version of
guide that came with ver 6 matlab.

I don't think there is any way to legally get
this software anymore, since the Mathworks no
longer sells it.

~Paul

Subject: Converting Matlab .m file into C code

From: John reilly

Date: 9 Oct, 2008 13:53:22

Message: 10 of 17

For those of you who want to convert MATLAB to C, the fundamental question is: Why? What do you hope to gain?

If you answer that, someone can tell you the best way to proceed.

Once upon a midnight dreary, someone posted in this forum a link to a tool that converts MATLAB to C. It is probably very expensive.

The MATLAB Compiler/Builder tools will generate a DLL that you can call from C/C++/C#/whatever. You can write your application, and distribute it with the DLL and the MATLAB Compiler Runtime (MCR) (170+MB) so that your users can run your app without needing MATLAB. The compiler can also generate an exe from your script, including a GUIDE script.

You can implement your MATLAB in a Simulink model (say an embedded MATLAB function block, which has some restrictions) and RealTime Workshop will generate C.

Walter covered all this, I know. But he could answer you a lot better if you'd tell him *WHY* you want to convert to C.

reilly.

Subject: Converting Matlab .m file into C code

From: Walter Roberson

Date: 9 Oct, 2008 17:54:13

Message: 11 of 17

John Reilly wrote:
> For those of you who want to convert MATLAB to C, the fundamental question is: Why?
> What do you hope to gain?

Experience suggests that when people ask for this, they are asking for one of three things:

A) A deployment tool similar in effect to the Matlab compiler, but without the cost associated
with the official Matlab compiler; Or

B) A deployment tool similar in effect to the Matlab compiler, but which produces *much* smaller
packages by only including the parts of Matlab that are "actually used" for the program;
said package should include a magic version of BLAS (and similar libraries) that is
simultaneously optimized for all possible target processors so the delivered executable will
"just work" (but of course, only the portions of those libraries that will actually be called
upon should be included); Or

C) A tool that will speed up their code by a factor of at least 20 (worst case), with 80 to 100 times
speedup -expected-, because "everyone knows" that Matlab is much slower than C.

> Walter covered all this, I know. But he could answer you a lot better if you'd tell him
> *WHY* you want to convert to C.

Unfortunately Walter has no "better" answer to any of the above situations than he has
already posted. Walter is a Mathworks customer, not a Mathworks employee, and Mathworks
typically doesn't tell him much more than "This will be fixed in a future release".

 

Subject: Converting Matlab .m file into C code

From: amir

Date: 16 Oct, 2008 13:05:04

Message: 12 of 17

"John Reilly" <jrhokie1.nospam@yahoo.com> wrote in message <gcl2ci$oq$1@fred.mathworks.com>...
> For those of you who want to convert MATLAB to C, the fundamental question is: Why? What do you hope to gain?
>
> If you answer that, someone can tell you the best way to proceed.
>
> Once upon a midnight dreary, someone posted in this forum a link to a tool that converts MATLAB to C. It is probably very expensive.
>
> The MATLAB Compiler/Builder tools will generate a DLL that you can call from C/C++/C#/whatever. You can write your application, and distribute it with the DLL and the MATLAB Compiler Runtime (MCR) (170+MB) so that your users can run your app without needing MATLAB. The compiler can also generate an exe from your script, including a GUIDE script.
>
> You can implement your MATLAB in a Simulink model (say an embedded MATLAB function block, which has some restrictions) and RealTime Workshop will generate C.
>
> Walter covered all this, I know. But he could answer you a lot better if you'd tell him *WHY* you want to convert to C.
>
> reilly.
Hi Reilly,

i need to make a exe or any other form of my m-file which could be run inside my Compaq visual Fortran6.6. is it possible to make it with Matlab7.6 ?
because i have tried a lot, but it seems CVF is not supported by your MATLAB version 2008a.
Amir

Subject: Converting Matlab .m file into C code

From: rustic.drive@gmail.com

Date: 31 Oct, 2008 15:45:57

Message: 13 of 17

On Oct 9, 1:54=A0pm, Walter Roberson <rober...@hushmail.com> wrote:
> B) A deployment tool similar in effect to theMatlabcompiler, but which pr=
oduces *much* smaller
> packages by only including the parts ofMatlabthat are "actually used" for=
 the program;
> said package should include a magic version of BLAS (and similar librarie=
s) that is
> simultaneously optimized for all possible target processors so the delive=
red executable will
> "just work" (but of course, only the portions of those libraries that wil=
l actually be called
> upon should be included); Or

> C) A tool that will speed up their code by a factor of at least 20 (worst=
 case), with 80 to 100 times
> speedup -expected-, because "everyone knows" thatMatlabis much slower tha=
nC.
>
> > Walter covered all this, I know. =A0But he could answer you a lot bette=
r if you'd tell him
> > *WHY* you want to convert toC.

Walter, thanks for your insight into Matlab compiler and Embedded
Matlab.
I am looking to convert some Matlab code into C. Reason : I want my
code to run as fast as possible. It will not be a real time app and
will sit on a PC. But execution time is important.
I have been researching this and spoke to Mathworks support recently.

This is what I learnt about Embedded Matlab:
-Will need to buy Real Time Workshop and Simulink ($ 7500+3000)
- Embedded Matlab (emlc) is limited
a. Not all functions can be translated
b. M code to be written to be compliant with emlc guidelines
c. Will not be as fast as normal C
d. <my thought> Unknown issues that may arise from machine generated
code

The support person seemed to suggest that Matlab compiler might be a
solution that runs faster.

From your experience,
     1. how reliable is C code generated by matlab
     2. how much of a pain is it to write m code that is Embedded
Matlab compliant
     3. do you know if a Matlab Compiler solution might run faster
than Embedded Matlab generated C
     4. How much faster is Embedded Matlab generated C than normal
Matlab
     5. Do you have any insights on the Parallel computing Toolbox?

Thank you,
Madhu

Subject: Converting Matlab .m file into C code

From: Mike

Date: 9 Dec, 2008 23:41:02

Message: 14 of 17

Can some body tel me the reason why the removed C generation from matlab?

I have always used matlab for signal and image process algorthim devlopment not running the actually program live. Once a working algorithm has been found I concerted to much faster compliers.

Because matlab is so slow it has to be run independly on a seperate C application to have a chance of running machine vision systems.
Now, I have no problem operating scilab for my work and it costs nothing and the only advatage matlab had was the abilty to generate C code. Matlab has effectly just removed its advantage of an automated method of going from protoype code to useful "real time" use.
I'd now have to do it manually so why the hell would you pay thousands of dollars for a liceance when it now requires the same work as the opensource application?

Do they actually think it is acceptable to need to install matlab componets on customers equipment? and have the application run far slower?

Subject: Converting Matlab .m file into C code

From: PSYCHOBUHLER

Date: 21 Feb, 2012 10:11:10

Message: 15 of 17

"Mike" wrote in message <ghmvmd$t52$1@fred.mathworks.com>...
> Can some body tel me the reason why the removed C generation from matlab?
>
> I have always used matlab for signal and image process algorthim devlopment not running the actually program live. Once a working algorithm has been found I concerted to much faster compliers.
>
> Because matlab is so slow it has to be run independly on a seperate C application to have a chance of running machine vision systems.
> Now, I have no problem operating scilab for my work and it costs nothing and the only advatage matlab had was the abilty to generate C code. Matlab has effectly just removed its advantage of an automated method of going from protoype code to useful "real time" use.
> I'd now have to do it manually so why the hell would you pay thousands of dollars for a liceance when it now requires the same work as the opensource application?
>
> Do they actually think it is acceptable to need to install matlab componets on customers equipment? and have the application run far slower?


It didn't been removed, but it make a part of a toolbox.
You can convert MATLAB code in C code with MATLAB Coder (toolbox) !

Subject: Converting Matlab .m file into C code

From: saranyageethu

Date: 31 May, 2012 17:18:07

Message: 16 of 17


> Is there a chance, in Matlab, to convert ".m file" into
> a "c code", to run it separately from Matlab?. How, if it
> is possible?
> Thank you
> saranya
>

Subject: Converting Matlab .m file into C code

From: Steven_Lord

Date: 31 May, 2012 17:43:36

Message: 17 of 17



"saranyageethu " <mtech-es-1@googlegroups.com> wrote in message
news:jq894f$jq$1@newscl01ah.mathworks.com...
>
>> Is there a chance, in Matlab, to convert ".m file" into a "c code", to
>> run it separately from Matlab?. How, if it is possible?

Look at MATLAB Coder or MATLAB Compiler depending on what you're trying to
do.

http://www.mathworks.com/products/matlab-coder/

http://www.mathworks.com/products/compiler/

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us