MATLAB to C code conversion [duplicate] - matlab

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Matlab to C or C++
is there any software to convert MATLAB code to c....

Yes. Embedded Matlab. Works well but only allows use of a subset of matlab functions.
http://www.mathworks.co.uk/products/featured/embeddedmatlab/
It is fine for converting your algorithms into C without having to worry about the possibility of errors being introduced in hand conversion. you will need a license for rtw.
http://www.mathworks.co.uk/products/rtw/

MATLAB is able to compile your .m files to binary files if you just want them to run faster:
http://www.mathworks.com/help/toolbox/compiler/mcc.html

If C++ is acceptable, you could try a library such as Armadillo, which provides C++ versions of many MATLAB functions. This allows for a relatively easy conversion of many functions.

Related

equivalent of the cwt (matlab) function for octave gui?

I'd like to use a Continuous wavelet transform (CWT) function in my octavegui code.
in Octave gui CWT is not available - is there equivalent .pkg to use a cwt?
fwt is available in the ltfat.pkg- but this seems to be something different than cwt.
According to the pdf from Prusa a form of a CWT is a Morlet or a mexican hat ("Continuous Wavelet Transfom – CWT (Morlet, Mexican hat, . . . ).= - is it possible to use a Morlet function in Octave? or is this something different?
any suggestions? (I am not a mathematician)
thank You
If none of these functions cover your needs, then you have two other options:
Use a c++ or java package within octave. Octave has external interfaces for both.
Use a simple python / julia / whatever scripts to perform specific calculations using your favourite wavelet package natively in that language, and use .mat converters to pass data (e.g. scipy.io.loadmat / savemat in python, and MAT.jl for julia) in and out of octave. Octave provides a 'python' function for executing python scripts for convenience (but this is no more than a nicely wrapped system call).
In any case, unfortunately that does mean you would need to know a bit of python / c++ / java respectively.
You can find a nice list of wavelet packages here.
Another thing you could try, is scilab. It is similar to octave (though not identical), and provides a wavelet package. There's also a module which allows intercommunication between octave and scilab.
Do have a look at the octave forge list first though. It's been a while, but last time I worked with wavelets I remember those covered my needs more than adequately.
PS. I did not mention Wavelab above, because their website does not mention this, and I don't know if it actually works on octave. Also, it seems a bit outdated. But in theory there's no reason why it wouldn't work, might as well give their .mex files a try if it seems relevant to you.
PS2. If something works for you, feel free to leave a comment here for future readers.

Matlab - Transforming my program into a GUI [duplicate]

This question already has answers here:
How can I program a GUI in MATLAB? [closed]
(4 answers)
Closed 6 years ago.
I wrote a code that: imports sequential files containing x- and y- columns, plots the data, fits the peaks using a 4-term gaussian, saves the plots as .png image files and finally exports all the variables into a table (called final). I want this program to be user friendly as non-programmers need to use it. Please let me know if it is possible. I would like to be directed towards the right path. As I do not know where to start.
There are thousands of resources on-line, but they are not specific. I would like to know if it is possible in my case to create a GUI, so I dont fish in an empty pond.
You might consider using "GUIDE" which is Matlab's GUI design environment. It's fairly easy to use. To start simply type:
guide
in the command window. There is a lot of documentation on how to make a gui with guide. I'd wager you could quickly come up with a gui that calls your script, or you could convert your script to a set of functions and call those from the gui.
http://www.mathworks.com/help/matlab/creating_guis/about-the-simple-guide-gui-example.html

How to use Matlab toolboxes consist of .p suffix? [duplicate]

This question already has an answer here:
How use pcode to regenerate a file in MatLab?
(1 answer)
Closed 6 years ago.
I have Matlab toolbox contains files with .p suffix.
I've set path it but when I've run main files of toolbox this error appeared :
p was generated prior to MATLAB version 7.5 (R2007b) and is
no longer supported. Use pcode to regenerate the file using MATLAB R2007b or later.
Is there any way to use this toolbox for newer versions of matlab like 2011 or newer?
A p-code file is an obfuscated version of an m-file that should not be readable by the recipient; however, MATLAB is still able to read and interpret these files as though they were the original (unobfuscated) m-files.
As the error states, an older version of MATLAB was used to generate the p-code files you have and therefore it may not be compatible with the version of MATLAB that you are using.
You would need to get a copy of the original m-files as there is no reliable way to "recompile" a p-code file. You would need to run pcode on the original m-files to generate new/compatible p-files.
This may require that you get in touch with the original developer.
That being said, it should just be a warning and you should be able to still use the files with the caveat that there may be unexpected behavior.

Alternatives to extrinsic functions such as imread and other functions during code generation in MATLAB

As you may know, extrinsic functions are not outputted during the code generation process. Are there alternatives to these functions and/or solutions to this problem? My code generation error report is shown below :
Code Generation Error Report
I am surprised that I can't output size and rgb2gray either. Since these are essential to my program, I cannot avoid them.
Help will be much appreciated!
This is a good question, and I see similar questions fairly frequently. As I started using MATLAB Coder, one of the biggest pitfalls was the constant search for supported functions. I sympathize with your frustration, and I have a few tips, having been through this.
First, to your direct question, while imread isn't supported by Coder, size and rgb2gray are. Probably Coder is complaining about these because they have been passed mxArrays from the call to imread, which is fine when it is extrinsic, but not ok for separate generation. That's just a guess. A very useful tool in writing code is the list of Coder supported functions: List of Functions supported in MATLAB Coder
But even with those two, to replace imread is not a tiny task. You'll have to find another library that supports the particular file you're working with, and then stitch that in using coder.ceval. Alternatively, if you can find a pure MATLAB implementation of it, that might help.
Are you targeting a pure C library or a MEX file? If you intend to use this code within the MATLAB environment, you can always use imread separately and then pass the data.
And now to some more general observations: MATLAB Coder isn't a perfect MATLAB to C translation system. It's extremely powerful, and I've been able to write some very large projects with it, but if what you want is the ability to run any MATLAB code without MATLAB around, you should look at MATLAB Compiler, a different add-on. There's a very good Q and A about this here: MATLAB Compiler vs MATLAB Coder
When writing projects in MATLAB Coder, it's really best to start from scratch, knowing you're targeting C code ultimately. There are so many gotchas in the conversion from MATLAB to C that you have to be always vigilant while writing the MATLAB code.
One tool that helps is to right-click on a file in the "Current Folder" list that usually resides on the left-side of the main window, and select "Check Code Generation Readiness." You'll get a great report of potential problems in the file. I recommend using this often.
Another useful tool is to always put the %#codegen tag into your code. This alerts the MATLAB editor that the .m file is intended for code generation, so it provides extra context-sensitive information while you're writing the file. This helps enormously.
The most commonly missing functions for code generation are file IO functions. There are some good reasons for this, but it's frustrating nonetheless.
When you stitch in external C code, you use the coder.ceval function, which can provide excellent access to external libraries. Using this well is a whole other topic, outside the scope of this question.
If you can indicate exactly what kind of files you're interested in reading (PNG, BMP, TIFF, etc.) perhaps someone may be able to identify a good external library for you to use.

MATLAB tutorial for programmers [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm getting some new students soon, who will be writing MATLAB code. They're new to MATLAB, but they have experience coding in Java and C++.
I'm going to have them go through the Getting Started section of the MATLAB help. In addition, I want to give a small tutorial with the goal to prevent them from making some of the most common mistakes people make when switching to MATLAB (e.g. "MATLAB starts counting at 1"), and show them some features that they may not be aware of when coming from other languages (e.g. "you can subtract a scalar directly from an array, and for vectors, there's bsxfun").
What are the most important things I should tell them?
I agree with previous answers, but I'd say indexing is the first and the most important and complex concept in studying MATLAB. I saw many C programmers starting with MATLAB just write loops, a lot of loops, something ridiculous like
for i=1:10
a(i)=i;
end
instead of simple a=1:10;.
So I'd suggest them to read about matrix programming concepts:
How to create simple vectors and matrices
Which variables can be used for indexing
How to create and apply indexes
Logical operations and functions, logical and numeric indexes (find function)
Indexing right and left side of expression
Difference between indexing numerical matrices and cell arrays
How to use indexes as output from different functions, like sort, unique, ismember, etc.
You cannot apply indexes to intermediate results
As for productivity, I would add that knowing how to use editor's cell mode is very useful.
Enough snippy comments, here's something of an answer too:
The Matlab desktop: what all the windows are for, dragging code from the history back into the command window, the variable inspector, etc.
Plotting: not just the plot command, but how to use the plot GUI tools, and how to create an M-file from a graphic.
M-files for scripts and functions, and the key differences between them.
M-Lint, the profiler.
Use Matlab as a vehicle for teaching the perils and pitfalls of floating-point arithmetic.
Getting help: at the command line, on the web, documentation, the file exchange, ...
Set path and the current working directory.
Importing data from files, exporting data to files, loading and saving.
That should be enough to keep them busy for an hour or so.
To clarify, I propose these topics to help you teach your students to avoid common Matlab errors including;
Unproductive use of the tool, retyping commands which can easily be recalled from the history, using C (or Java) style file reading commands instead of uuimport, slowly typing scripts to draw graphics when Matlab can do it for you, wondering what all the little orange lines in the editor right margin mean and the squiggly underlines, trying to figure things out for themselves when the help facilities could tell them, tons of other stuff that many much more experience Matlab users have taken ages to learn.
Floating point arithmetic is not real.
and probably a lot of other stuff too.
For those coming from C-family languages, the element-wise operators are new. It took me a couple of months to discover the ./ and .* operators. Before that, I used to write for loops for element-wise operations. So perhaps that's something that should be pointed out.
With respect to unexpected or non-intuitive MATLAB features that may cause them confusion, there are some good pointers in this question:
Corner Cases, Unexpected and Unusual MATLAB
With respect to cool time-saving/efficiency tricks, this other question has some nice examples:
What are your favourite MATLAB/Octave programming tricks?
And for a few potentially more advanced topics, you can refer to the answers to this question:
MATLAB interview questions?
Now for my $0.02. Based on the sorts of questions I've seen asked most frequently on SO, I'd say you will want to make sure they have a good understanding of the following concepts:
Reading and writing data files of different formats, such as using CSVREAD, DLMREAD, TEXTREAD, FREAD, FSCANF, LOAD, and all their write equivalents.
How to deal effectively with cell arrays.
The different image formats, how these are represented, and how to modify them (which will involve a discussion of various data types and how to deal with multi-dimensional arrays).
How to use handle graphics to control the appearance of various graphics objects.
And here are some neat features that are already implemented in MATLAB that may save them some time and effort:
Functions for performing various array operations, like KRON, DIAG, and TRIU.
Functions to create specialized matrices, like HANKEL and TOEPLITZ.
Predefined dialog boxes, like UIGETFILE and INPUTDLG.
MATLAB is conceptually in some ways very different from other languages you mentioned:
cells are used were Java uses upcasting
global and persistent variables are static in Java
gui handles being just numbers of type double
nested functions are closures, neither Java nor C/C++ has such feature
seldom used private and #TYPE folders for visibility scoping
array handling tricks
very easy interoperability with Java/COM/.Net using MATLAB syntax
variadic function arguments, handling of function arguments with varargin / varargout
memory management