How to locate where a built-in function is defined? - matlab

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.

Related

Overwrote built in function - Standard deviation

I want to have a std.m file for the standard deviation. It is in data fun toolbox, but, by mistake, I changed the code and the std command is not working anymore. How can I run the original std (standard deviation) command?
Taking all the comments out, the function std.m is actually extremely simple:
function y = std(varargin)
y = sqrt(var(varargin{:}));
This is the definition of the standard deviation: the square root of the Variance.
Set built-in functions to Read-Only
Now don't go breaking the var.m file because it is more complex and I wonder if there would be copyright issue to display the listing here.
To avoid the problem of breaking built-in files, it is advisable to set all your Matlab toolbox files as Read Only. I remember old Matlab installer giving the option to do that at install time. I don't know if the installer still offers the option, but if not it is extremely easy to do it manually (on Windows, just select your folders, right-click Properties, tick read only and accept to propagate the property to all subfolders and files).
Overloading
Once this is done, your built-in files are safe. You can still modify the default behavior of a built-in function by overloading it. This consist in writing a function with the same name and arrange for it to be called before the default function (your overload function becomes the default one).
This article explain how to overload user functions.
Matlab does not recommend to directly overload the built-in functions (rather call it another name like mySTD.m for example), but if you insist it is perfectly feasible and still a much better option than modifying the built-in function... at least the original function is still intact somewhere.

How to include excluded toolbox m-files in MATLAB compiler

I want to use Fuzzy Logic Toolbox in C#. To do this, I created a NET-library using deploytool, but it does not include the file fuzzy.m, which I need to work. And in the log mccExcludedFiles.log the following information:
This file contains the list of various toolbox functions that are not
included in the CTF file. An error will be thrown if any of these functions
are called at run-time. Some of these functions may be from toolboxes
that you are not using in your application. The reason for this is that
these toolboxes have overloaded some methods that are called by your code.
If you know which toolboxes are being used by your code, you can use the -p
flag with the -N flag to list these toolboxes explicitly. This will
cause MATLAB Compiler to only look for functions in the specified toolbox
directories in addition to the MATLAB directories. Refer to the MCC
documentation for more information on this.
C:\Program Files\MATLAB\R2010b\toolbox\fuzzy\fuzzy\fuzzy.m
called by D:\MyFolder\VNTU\bakal\matlabAndCs\ShowFuzzyDesigner.m
(because of toolbox compilability rules)
How do I include this excluded fuzzy.m file in the compilation?
The command fuzzy launches the Fuzzy Inference Systems editor, a GUI supplied with Fuzzy Logic Toolbox. Compilation of Toolbox GUIs with MATLAB Compiler is typically not supported, and as detailed in the documentation for MATLAB Compiler, this is true of the GUIs within Fuzzy Logic Toolbox.
I must say, I think the message you're getting in the mccExcludedFiles.log file is mostly misleading - all of those things could cause a file to have been excluded, but in this case the only relevant bit is at the end, "(because of toolbox compilability rules)".
You might want to look into how to build a fuzzy system using the line code functions supplied with the fuzzy toolbox, and not the GUI. This walkthrough gives you a pretty good handle on building a Mamdani inference system using the line code tools. I am not positive how these translate into C# code, but I think there should be equivalent libraries therein.
If you cannot find a natural way to implement the MATLAB routines in C#, then you could look at this discussion which links some free fuzzy libraries for C#. I think one of the links is broken, but the other three load just fine.

Mex or Compile (mcc) Matlab function that uses toolkits

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

Matlab code after compilation

I am totally a newbie in Matlab
I want to ask that when we write a program in Matlab software or IDE and save it with a
.m (dot m) file and then compile and execute it, then that .m (dot m) file is converted into which file? I want to know this because i heard that matlab is platform independent and i did google this but i got converting matlab file to C, C++ etc
Sorry for the silly question and thanks in advance.
Matlab is an interpreted language. So in most cases there is no persistent intermediate form. However, there is an encrypted intermediate form called pcode and there are also the MATLAB compiler and MATLAB coder which delivers code in other high level languages such as C.
edit:
pcode is not generated automatically and should be platform/version independent. But it's major purpose is to encrypt the code, not to compile it (although, it does some partial compilation). To use pcode, you still need the MATLAB environment installed, so in many ways it acts like interpreted code.
But from your follow-up question I guess you don't quite understand how MATLAB works. The code gets interpreted (although with a bit of Just-In-Time Compilation), so there is no need for a persistent intermediate code file: the actual data structures representing your code are maintained by MATLAB. In contrast to compiled languages, where your development cycle is something like "write code, compile & link, execute", the compilation (actually: interpretation) step is part of the execution, so you end up with "write code, execute" in most of the cases.
Just to give you some intuitive understanding of the difference between a compiler and an interpreter. A compiler translates a high level language to a lower level language (let's say machine code that can be executed by your computer). Afterwards that compiled code (most likely stored in a file) is executed by your computer. An interpreter on the other hand, interprets your high level code piece by piece, determining what machine code corresponds to your high level code during the runtime of the program and immediately executes that machine code. So there is no real need to have a machine code equivalent of your entire program available (so in many cases an interpreter will not store the complete machine code, as that is just wasted effort and space).
You could look at interpretation more or less as a human would interpret code: when you try to manually determine the output of some code, you follow the calculations line by line and keep track of your results. You don't generally translate that entire code into some different form and afterwards execute that code. And since you don't translate the code entirely, there is no need to persistently store the intermediate form.
As I said above: you can use other tools such as MATLAB coder to convert your MATLAB code to other high languages such as C/C++, or you can use the MATLAB compiler to compile your code to executable form that depends on some runtime libraries. But those are only used in very specific cases (e.g. when you have to deploy a MATLAB application on computers/embedded devices without MATLAB, when you need to improve performance of your code, ...)
note: My explanation about compilers and interpreters is a quick comparison of the archetypal interpreter and compiler. Many real-life cases are somewhere in between, e.g. Java generally compiles to (JVM) bytecode which is then interpreted by the JVM and something similar can be said about the .NET languages and its CLR.
Since MATLAB is an interpreter, you can write code and just execute it from the IDE, without compilation.
If you want to deploy your program, you can use the MATLAB compiler to create an stand-alone executable or a shared library that you can use in a C++ project. On Windows, MATLAB code would compile to an .EXE file or a .DLL file, respectively.

Matlab code to .mex using code package

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.