Somewhere between R2010b and R2018a, Matlab seems to have removed dos as a built-in function.
In R2010b:
>> which dos
built-in (C:\Program Files\MATLAB\R2010b\toolbox\matlab\general\dos)
In R2018a:
>> which dos
C:\Program Files\MATLAB\R2018a\toolbox\matlab\general\dos.m
Why, and when, was this change made?
If you open the file dos.m you can notice that is a wrapper to the built-in function
system so the answar might be that MathWorks has improved the system function to run correctly both Windows command and Unix command so a built-in function for a wrapper is useless due the definition of built-in function:
A built-in function is part of the MATLAB executable. MATLAB does not implement these functions in the MATLAB language. Although most built-in functions have a .m file associated with them, this file only supplies documentation for the function.
Related
In MATLAB, there are roughly 3 ways to define functions: non-comment-only .m files, .p files, and compiled code (e.g. DLL, MEX).
Knowing where a function is defined could be helpful in several cases, such as when a breaking change was introduced to some function outside our control, and we'd like to try to revert to an old version in the hopes of getting our code working again; or when trying to reverse-engineering some undisclosed algorithm.
The which function is usually very good at identifying function definitions and their locations (which works for .m, .p and MEX), but isn't very useful when it comes to shared library functions, where (at best) it points to a comment-only documentation file:
>> which _mcheck
built-in (undocumented)
>> which svd
built-in (D:\Program Files\MATLAB\R2019a\toolbox\matlab\matfun\svd)
If so, assuming a function found within a shared library is called during the execution of my code, how can I locate the specific file (DLL) that contains it?
It turns out that dbstop can be used for this. For example:
>> which svd
built-in (D:\Program Files\MATLAB\R2019a\toolbox\matlab\matfun\svd)
>> dbstop svd
Warning: Entering debug mode is only supported within running MATLAB code files.
Warning: MATLAB debugger can only stop in MATLAB code files, and "libmwmathlinalg>svd" is not a MATLAB code file.
Instead, the debugger will stop at the point right before "libmwmathlinalg>svd" is called.
From there's it's just a matter of finding a file called libmwmathlinalg (with the relevant extension) - which isn't a difficult task if your drive is indexed.
I was wondering if there is a way to create a '.exe' file from ' .m' file in MATLAB, such that it can be run in machine which does not have MATLAB (like it can be done in C, C++).
I know writing a MATLAB function is one way, but I am not sure if it can run in machine without MATLAB.
Also I would like to hide my code and just create a script which can be run by a user using his own data files.
The Matlab Compiler is the standard way to do this. mcc is the command. The Matlab Runtime is required to run the programs; I'm not sure if it can be directly integrated with the executable or not.
If you have MATLAB Compiler installed, there's a GUI option for compiling. Try entering
deploytool
in the command line. Mathworks does a pretty good job documenting how to use it in this video tutorial: http://www.mathworks.com/products/demos/compiler/deploytool/index.html
Also, if you want to include user input such as choosing a file or directory, look into
uigetfile % or uigetdir if you need every file in a directory
for use in conjunction with
guide
Try:
mcc -m yourfile
Also see help mcc
If your code is more of a data analysis routine (vs. visualization / GUI), try GNU Octave. It's free and many of its functions are compatible with MATLAB. (Not 100% but maybe 99.5%.)
mcc -?
explains that the syntax to make *.exe (Standalone Application) with *.m is:
mcc -m <matlabFile.m>
For example:
mcc -m file.m
will create file.exe in the curent directory.
It used to be possible to compile Matlab to C with older versions of Matlab. Check out other tools that Matlab comes with.
Newest Matlab code can be exported as a Java's jar or a .Net Dll, etc. You can then write an executable against that library - it will be obfuscated by the way. The users will have to install a freely available Matlab Runtime.
Like others mentioned, mcc / mcc.exe is what you want to convert matlab code to C code.
The "StandAlone" method to compile .m file (or files) requires a set of Matlab published library (.dll) files on a target (non-Matlab) platform to allow execution of the compiler generated .exe.
Check MATLAB main site for their compiler products and their limitations.
I developed a non-matlab software for direct compilation of m-files (TMC Compiler). This is an open-source converter of m-files projects to C. The compiler produces the C code that may be linked with provided open-source run-time library to produce a stand-alone application. The library implements a set of build-in functions; the linear-algebra operations use LAPACK code. It is possible to expand the set of the build-in functions by custom implementation as described in the documentation.
table() has been a standard function in MATLAB since R2013b. As far as I can see from the documentation, there's nothing special about table, compared to sum, cell, struct, or any other builtin function.
However, when I try to run the function using builtin('table',var1,...varN) I get an error saying:
Error using builtin
Cannot find builtin function 'table'
Investigating this further shows that it is in fact not considered a builtin function:
which('cell')
built-in (C:\Program Files\MATLAB\R2014b\toolbox\matlab\datatypes\cell)
which('table')
C:\Program Files\MATLAB\R2014b\toolbox\matlab\datatypes\#table\table.m % table constructor
|
Not builtin
Investigating further:
which cell2mat
C:\Program Files\MATLAB\R2014b\toolbox\matlab\datatypes\cell2mat.m
which mat2cell
C:\Program Files\MATLAB\R2014b\toolbox\matlab\datatypes\mat2cell.m
which table2array
C:\Program Files\MATLAB\R2014b\toolbox\matlab\datatypes\table2array.m
which struct2cell
built-in (C:\Program Files\MATLAB\R2014b\toolbox\matlab\datatypes\#struct\struct2cell) % struct method
which cell2struct
built-in (C:\Program Files\MATLAB\R2014b\toolbox\matlab\datatypes\#cell\cell2struct) % cell method
So, cell is builtin while table is not. cell2struct is builtin while cell2mat is not.
Why is this, and is there a simple way to call overloaded standard functions that aren't considered builtin by MATLAB?
If you think the why part is "too broad", please disregard it and jump to the last part of the question.
You can find the native function(s) by something like:
allTables = which ( '-all', 'table' )
allTables(cell2mat(strfind ( allTables, matlabroot )))
Its not fullproof and for some functions (e.g. sum) there are a lot in the root folders...
The documentation page for builtin gives a clear definition:
A built-in function is part of the MATLAB executable. MATLAB does not implement these functions in the MATLAB language. Although most built-in functions have a .m file associated with them, this file only supplies documentation for the function.
Environment:
Matlab R2012a (I have access to others if necessary)
All Toolboxes/Compiler installed
Ubuntu 12.04 64bit and/or Windows 7 64bit
I am working with the source for a software package written in Matlab (unfortunately its proprietary so no code examples...sorry), and one function briefly uses the Control System Toolbox and the Signal Processing Toolbox.
I have no problem running the code on my personal computer because I have every toolbox installed, however I would like to compile (mex or mcc) JUST the function using those two toolboxes. The goal, of course, is to run the software on a machine without those toolboxes, while leaving the remaining code open to change.
According to matlab, they have no problem with you compiling code that uses almost any toolbox. Here is the list of toolboxes that support mcc compilation:
http://www.mathworks.com/products/compiler/supported/compiler_support.html
The problem arises in that mcc no longer allows compiling with the -x option to create a mex-ed version of the function, so I'm forced to create a C executable (maybe? hopefully not). This particular function takes large matrices as parameters (impractical to write as a command line argument) and returns a structure of cell arrays.
The only way around this (as I see it now) would be to write the arguments (large matrices) to the hard drive in a binary .mat file , have the compiled C binary read in the arguments, run the algorithm, and finally save the return values in another .mat for the parent thread to load back into memory.
This seems totally impractical. I would greatly appreciate alternative suggestions. Please let me know if anything here in unclear. Thanks in advance!
[Edit 1] The codegen package does not support tf.m. It seems like this should be possible (and used to be possible with the mex -x option), but I'm at a loss. Any suggestions would be greatly appreciated!
I think the reason -x is not supported anymore is the fact that Matlab now has a product called "coder", which converts .m files to .c files and can also create .mex files from "suitable" .m files using the option -args to specify the input arguments: http://www.mathworks.com/videos/generating-c-code-from-matlab-code-68964.html
I would like to optimize a function written in Matlab by converting the code to C\C++. The result should be callable from within matlab, as it is a small part of a larger matlab code.
For example, converting my function to C code wrapped in a .mex file would work.
I heard matlab coder package can help with that.
As I am unfamiliar with this package, what is the quickest way to achieve this?
If you have a license for MATLAB Coder, then, yes, that is the correct package to use. The function you're looking for is codegen. There are restrictions on what can be used in code generation: to see if your function meets those restrictions, add the tag %#codegen to the beginning of your function as shown below
function foo(bar) %#codegen
<your code here>
and open the function file in the MATLAB editor. The tag tells the editor to check that the code complies with the rules for code generation. Once the editor shows that your code complies with those rules, generating the mex file may be as easy as
>> codegen foo
which would generate a mex-file, foo_mex in the current folder. For your particular function you may need to use some of the optional arguments for codegen to generate the mex-file properly.