Matlab
FromMatlab
translate a MATLAB(R) expression to Maple
Calling Sequence
Parameters
Description
Examples
FromMatlab(s)
FromMatlab(s, evaluate )
FromMatlab(s, string )
FromMatlab(s, AddTranslator=false )
s
-
string
evaluate
(optional) truefalse
AddTranslator
FromMatlab converts a statement written in MATLAB® syntax to an equivalent statement in Maple syntax.
By default the FromMatlab command shows the Maple syntax conversion and executes the converted code. Specifying the evaluate option will just do the evaluation without showing the code conversion first.
When the string option is specified the given MATLAB® expression will be translated to a Maple input string. It is generally better to first convert a MATLAB® expression to a string, so you can examine the translation, confirm its accuracy, and make necessary adjustments before leaping into an evaluation.
When the AddTranslator option is set to false, the translator bypasses the AddTranslator extension mechanism and gives a general MATLAB® to Maple syntax translation without attempting to interpret the meaning of MATLAB® function calls.
FromMatlab only understands a subset of the MATLAB® language. Among other things, structs and classes are not handled by the translator. Beyond the basic syntax, FromMatlab also does not know about every MATLAB® function. There are built-in conversions for over a hundred common MATLAB® commands, but some of the translations only cover the most common cases in those commands. Missing commands can be supplied by the user via AddTranslator.
If a translated name is not matched by anything known to AddTranslator, but does exactly match a protected name in Maple, the name will be prepended with m_. For example, FromMatlab("expand(x)") will generate m_expand(x) so as not to conflict with Maple's definition of expand. An exact match against an unprotected name does not cause m_ to be prepended. In this case the name is left as is.
The FromMatlab command is primarily an aid to learning how to write Maple programs for someone who is used to the MATLAB® language. With this in mind, translated code tries not to hide the "glue" -- code that figures out which option is wanted and dispatches to one or more Maple commands.
Another goal is to produce commands that will do exactly in Maple what was done in MATLAB®. Some commands therefore tend to be fairly verbose on translation and may contain things like on-the-fly procedure constructions. These wordy translations are probably not great examples of how Maple code would be constructed, and would not be written as such had they been handwritten from scratch in Maple. They may even be slower as a result. Consider the translation of sin(x). Since Maple can't know what value x is at translation time, it has to assume it could be an array or a constant. The resulting translation will take both into account and construct a call to map. Writing the code by hand, the author would know what x is intended to be at that time, and call the most appropriate function directly.
After translating MATLAB® code to Maple, it might be a good idea to look through the code and prune away cases that will never happen. The code will usually become easier to follow and more efficient.
Note that backslash is Maple's escape character. Inside a string, "\n" is a single character newline, and "\\n" is two characters -- a backslash and the letter "n". So, FromMatlab("A.\B"); has the 3 character sequence "A.B" for an argument as "\B" resolves to the letter "B". Use FromMatlab("A.\\B"); to get elementwise left divide. Code read from a file via FromMFile does not need its backslashes escaped.
In MATLAB®, variables like i and pi are initialized to special constant values, but can be reassigned to any value. In order to mimic this behavior the translator converts i to Matlab_i instead of I. Thus this variable can be used as, for example, a control variable in a loop.
with⁡Matlab:
FromMatlab⁡[ 1 2 ; 3 4]
Evaluating: Matrix([[1, 2], [3, 4]] );
1234
FromMatlab⁡A .* B
Evaluating: A *~ B;
A⁢B
FromMatlab⁡A .* B,string
A *~ B;
FromMatlab⁡function [x] = mysum(varargin)\n x = sum([varargin{:}])
Evaluating: mysum := proc( varargin ) uses ArrayTools; local x; x := ArrayTools:-AddAlongDimension(ArrayTools:-Concatenate(2, args)); return x; end proc;
procvararginlocalx;x ≔ ArrayTools:-AddAlongDimension⁡ArrayTools:-Concatenate⁡2,args;returnxend proc
mysum⁡1,2,3
6
See Also
Matlab[AddTranslator]
Matlab[FromMFile]
rtable_indexing
Download Help Document