CodeGeneration - Maple Programming Help

Online Help

All Products    Maple    MapleSim

Home : Support : Online Help : Programming : Input and Output : Translation : CodeGeneration/Matlab





translate Maple code to MATLAB(R) code


Calling Sequence




Calling Sequence

Matlab(x, cgopts)




expression, list, rtable, procedure or module



(optional) one or more CodeGeneration options



The Matlab command translates Maple code to MATLAB® code.


- If the parameter x is an algebraic expression, then a MATLAB® statement assigning the expression to a variable is generated.


- If x is a list, rtable or Maple Array of algebraic expressions, then a sequence of MATLAB® statements assigning the elements to a MATLAB® array is produced.


- If x is a list of equations nm=expr where nm is a name and expr is an algebraic expression, this is understood to mean a sequence of assignment statements.  In this case, the equivalent sequence of MATLAB® assignment statements is generated.


- If x is a procedure, then a MATLAB® module is generated containing a function equivalent to the procedure.


- If x is a module, then the closest equivalent MATLAB® code is generated, as described on the MatlabDetails help page.


The parameter cgopts may include one or more CodeGeneration options, as described in CodeGenerationOptions.


For more information about how the CodeGeneration package translates Maple code to other languages, see Translation Details. For more information about translation to MATLAB® in particular, see MatlabDetails.


For a description of the options used in the following examples, see CodeGenerationOptions.


Translate a simple expression and assign to the name ``w'' in the target code.


w = -2 * x * z + y * z + x;

Translate a list and assign to an array with name ``w'' in the target code.


w = [x 2 * y; 5 z;];

Translate a computation sequence.  Optimize the input first.



s = 0.10e1 + x;
t1 = log(s);
t2 = exp(-x);
t = t1 * t2;
r = x * t + t2;

Declare that x is a float and y is an integer.  Return the result in a string.


scg = x + y + 0.1e1;


Translate a procedure.  Assume that all untyped variables have type integer.

f := proc(x, y, z) return x*y-y*z+x*z; end proc:


function freturn = f(x, y, z)
  freturn = x * y - y * z + x * z;

Translate a procedure containing an implicit return.  A new variable is created to hold the return value.

f := proc(n)
  local x, i;
  x := 0.0;
  for i to n do
    x := x + i;
  end do;
end proc:


function freturn = f(n)
  x = 0.0e0;
  for i = 1:n
    x = x + i;
    cgret = x;
  freturn = cgret;

Translate a procedure accepting an Array as a parameter.  Note that the indices are renumbered so that the MATLAB® array starts at index 1.

f := proc(x::Array(numeric, 5..7))
  return x[5]+x[6]+x[7];
end proc:


function freturn = f(x)
  freturn = x(1) + x(2) + x(3);

Translate a module with one exported and one local procedure.

m := module() export p; local q;
    p := proc(x,y) if y>0 then trunc(x); else ceil(x); end if; end proc:
    q := proc(x) sin(x)^2; end proc:
end module:


% m/p.m:
function preturn = p(x, y)
  if (0 < y)
    preturn = fix(x);
    preturn = ceil(x);

% m/private/q.m:
function qreturn = q(x)
  qreturn = sin(x) ^ 2;

Translate a linear combination of hyperbolic trigonometric functions.


cg0 = 0.2e1 * cosh(x) - 0.7e1 * tanh(x);

Translate a procedure with no return value containing a printf statement.

f := proc(a::integer, p::integer)
  printf("The integer remainder of %d divided by %d is: %d\n", a, p, irem(a, p));
end proc:


function freturn = f(a, p)
  disp(sprintf('The integer remainder of %d divided by %d is: %d
',a,p,mod(a, p)));

See Also







Download Help Document

Was this information helpful?

Please add your Comment (Optional)
E-mail Address (Optional)
What is ? This question helps us to combat spam