Matlab[FromMatlab]  translate a MATLAB(R) expression to Maple

Calling Sequence


FromMatlab(s)
FromMatlab(s, evaluate )
FromMatlab(s, string )
FromMatlab(s, AddTranslator=false )


Parameters


s



string

evaluate



(optional) truefalse

string



(optional) truefalse

AddTranslator



(optional) truefalse





Description


•

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 builtin 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 onthefly 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.



Examples


>

$\mathrm{with}\left(\mathrm{Matlab}\right)\:$

>

$\mathrm{FromMatlab}\left(''[\; 1\; 2\; ;\; 3\; 4]''\right)$

Evaluating:
Matrix([[1, 2], [3, 4]] );
 
$\left[\begin{array}{rr}{1}& {2}\\ {3}& {4}\end{array}\right]$
 (1) 
>

$\mathrm{FromMatlab}\left(''A\; .*\; B''\right)$

>

$\mathrm{FromMatlab}\left(''A\; .*\; B''\,\mathrm{string}\right)$

>

$\mathrm{FromMatlab}\left(''function\; [x]\; =\; mysum(varargin)\; x\; =\; sum([varargin\{:\}])''\right)$

Evaluating:
mysum := proc( varargin )
uses ArrayTools;
local x;
x := ArrayTools:AddAlongDimension(ArrayTools:Concatenate(2, args));
return x;
end proc;
 
${\mathbf{proc}}\left({\mathrm{varargin}}\right)\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{local}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{x}{\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{x}{:=}{\mathrm{ArrayTools}}{:}{\mathrm{AddAlongDimension}}{}\left({\mathrm{ArrayTools}}{:}{\mathrm{Concatenate}}{}\left({2}{\,}{\mathrm{args}}\right)\right){\;}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{x}\phantom{\rule[0.0ex]{0.5em}{0.0ex}}{\mathbf{end\; proc}}$
 (4) 
>

$\mathrm{mysum}\left(1\,2\,3\right)$


