Matlab 转 C 或 C++

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4166755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:41:50  来源:igfitidea点击:

Matlab to C or C++

c++cmatlab

提问by emma

I am working on an image processing project using Matlab. We should run our program (intended to be an application) on a cell phone.We were then asked to convert our code into C or C++ language so we get a feel of how long it would take for execution and then choose a platform. So far we didn't figure out how to do this conversion.. Any ideas of what to do to convert Matlab to C or C++??

我正在使用 Matlab 进行图像处理项目。我们应该在手机上运行我们的程序(打算成为一个应用程序)。然后我们被要求将我们的代码转换成 C 或 C++ 语言,以便我们了解执行所需的时间,然后选择一个平台。到目前为止,我们还没有弄清楚如何进行这种转换。关于如何将 Matlab 转换为 C 或 C++ 的任何想法?

回答by Stuart Golodetz

The first thing you need to realise is that porting code from one language to another (especially languages as different as Matlab and C++) is generally non-trivial and time-consuming. You need to know both languages well, and you need to have similar facilities available in both. In the case of Matlab and C++, Matlab gives you a lot of stuff that you just won't have available in C++ without using libraries. So the first thing to do is identify which libraries you're going to need to use in C++. (You can write some of the stuff yourself, but you'll be there a long time if you write allof it yourself.)

您需要意识到的第一件事是,将代码从一种语言移植到另一种语言(尤其是与 Matlab 和 C++ 不同的语言)通常非常重要且耗时。您需要精通两种语言,并且需要在两种. 在 Matlab 和 C++ 的情况下,Matlab 为您提供了许多在不使用库的情况下在 C++ 中无法获得的东西。因此,首先要做的是确定您将需要在 C++ 中使用哪些库。(你可以自己写一些东西,但如果你自己写所有的东西,你会在那里很长时间。)

If you're doing image processing, I highly recommend looking into something like ITK at http://www.itk.org-- I've written my image processing software twice in C++, once without ITK (coding everything myself) and once with, and the version that used ITK was finished faster, performed better and was ten times more fun to work on. FWIW.

如果您正在进行图像处理,我强烈建议您在http://www.itk.org 上查看类似 ITK 的内容——我已经用 C++ 编写了两次我的图像处理软件,一次没有 ITK(自己编写所有代码),一次使用 ITK 的版本完成得更快,性能更好,并且工作的乐趣增加了十倍。FWIW。

回答by Stefan

Matlab can gererate C code for you.

Matlab 可以为您生成 C 代码。

See: http://www.mathworks.com/products/featured/embeddedmatlab/

请参阅:http: //www.mathworks.com/products/featured/embeddedmatlab/

The generated code does however depend on matlab libraries. So you probably can't use it for a cell phone. But it might save you some time anyways.

然而,生成的代码确实依赖于 matlab 库。所以你可能无法将它用于手机。但无论如何它可能会为您节省一些时间。

回答by MasterHD

I also used the MATLAB Coderto convert some functions consisting of a few hundred lines of MATLAB into C. This included using MATLAB's eigenvalue solver and matrix inversion functions.

我还使用MATLAB Coder将一些由几百行 MATLAB 组成的函数转换为 C。这包括使用 MATLAB 的特征值求解器和矩阵求逆函数。

Although Coder was able to produce C code (which theoretically was identical), it was very convoluted, bloated, impossible to decipher, and appeared to be extremely inefficient. It literally created about 10x as many lines of code as it should have needed. I ended up converting it all by hand so that I would actually be able to comprehend the C code later and make further changes/updates. This task however, can be very tedious/dangerous, as the array indexing in Matlab is 1-based and in C it's 0-based. You're likely to add bugs into the code, as I experienced. you'll also have to convert any vector/matrix arithmetic into loops that handle scalars (or use some type of C matrix algebra package)

尽管 Coder 能够生成 C 代码(理论上是相同的),但它非常复杂、臃肿、无法破译,而且效率极低。它实际上创建了它应该需要的大约 10 倍的代码行。我最终手动转换了所有内容,以便我以后能够真正理解 C 代码并进行进一步的更改/更新。然而,这项任务可能非常乏味/危险,因为 Matlab 中的数组索引是基于 1 的,而在 C 中它是基于 0 的。正如我所经历的,您可能会在代码中添加错误。您还必须将任何向量/矩阵算术转换为处理标量的循环(或使用某种类型的 C 矩阵代数包)

回答by gnovice

The MathWorks provides a product called MATLAB Coderthat claims to generate "readable and portable C and C++ code from MATLAB? code". I haven't tried it myself, so I can't comment on how well it accomplishes these goals.

MathWorks 提供了一个名为MATLAB Coder的产品,声称可以从 MATLAB 生成“可读且可移植的 C 和 C++ 代码?代码”。我自己没有尝试过,所以我无法评论它实现这些目标的程度。

With regard to the Image Processing Toolbox, this list(presumably for R2016b) shows which functions have been enabled for code generation and any limitations they may have.

关于Image Processing Toolbox此列表(大概适用于 R2016b)显示了哪些函数已启用代码生成以及它们可能具有的任何限制。

回答by Khoa

Matlab has a tool called "Matlab Coder" which can convert your matlab file to C code or mex file. My code is relatively simple so it works fine. Speed up gain is about 10 times faster. This saves me time coding a few hundreds lines. Hope it's helpful for you too

Matlab 有一个名为“Matlab Coder”的工具,可以将您的 matlab 文件转换为 C 代码或 mex 文件。我的代码相对简单,所以工作正常。加速增益大约快 10 倍。这节省了我编写数百行代码的时间。希望对你也有帮助

Quick Start Guide for MATLAB Coder Confirmation

MATLAB 编码器确认快速入门指南

The links describe the process of converting your code in 3 major steps:

这些链接描述了通过 3 个主要步骤转换代码的过程:

  • First you need to make a few simplifications in your present code so that it would be simple enough for the Coder to translate.

  • Second, you will use the tool to generate a mex file and test if everything is actually working.

  • Finally you would change some setting and generate the C code. In my case, the C code has about 700 lines including all the original matlab code (about 150 lines) as comments. I think it's quite readable and could be improve upon. However, I already get a 10 times speed up gain from the mex file anyway. So this is definitely a good thing.

  • 首先,您需要对当前代码进行一些简化,以便编码器可以轻松进行翻译。

  • 其次,您将使用该工具生成一个 mex 文件并测试是否一切正常。

  • 最后,您将更改一些设置并生成 C 代码。就我而言,C 代码有大约 700 行,包括所有原始 matlab 代码(大约 150 行)作为注释。我认为它的可读性很强,可以改进。但是,无论如何,我已经从 mex 文件中获得了 10 倍的加速增益。所以这绝对是一件好事。

We can't not be sure that this will work in all case but it's definitely worth trying.

我们不能确定这在所有情况下都有效,但绝对值得一试。

回答by AudioDroid

I remember there is a tool to export m-files as c(++)-files. But I could never get that running. You need to add some obscure MATLAB-headers in the c/c++code, ... And I think it is also not recommended.

我记得有一个工具可以将 m 文件导出为 c(++) 文件。但我永远无法让它运行。您需要在 c/c++ 代码中添加一些晦涩的 MATLAB 头文件,...我认为也不推荐这样做。

If you have running MATLAB-code, it shouldn't take too much effort to do the conversion "by hand". I have been working on several project where MATLAB was used and it was never consider to use any tools to convert the code to C/C++. It was always done "by hand".

如果您已运行 MATLAB 代码,则“手动”进行转换不应该花费太多精力。我一直在做几个使用 MATLAB 的项目,从来没有考虑使用任何工具将代码转换为 C/C++。它总是“手工”完成。

I believe to have been the only one who ever investigate into using a tool.

我相信他是唯一一个研究过使用工具的人。

回答by prolink007

Well there is not straight conversion from matlab to c/c++ You will need to understand the language and the differences between matlab and c/c++ and then start coding it in c/c++. Code a little test a little until it works.

好吧,没有从 matlab 直接转换到 c/c++ 你需要了解语言以及 matlab 和 c/c++ 之间的区别,然后开始用 c/c++ 编码。Code a little test一点,直到它工作。