You are on page 1of 6

MATLAB Overview

Functions have syntax as below:


function [output_arguments] = function_name(input_arguments)
%Code goes here (% used to comment in Matlab)
end

code sample:
function[c] = add(a,b)
c=a+b;
end

To call the above function create a excersice.m file as below:


Exercise.m
a=5;
b=4;
c=add(a,b);
disp(c);

To execute the exercise, you need to call the exercise.m file from matlab/octave
commandline.

Function Handle:
A function handle is a MATLAB data type that stores an association to a function.
Syntax:
f = @function_name
example:
function y = computeSquare(x)
y = x.^2;
end

The above function can be invoked using below code:


f = @computeSquare;
a = 4;

b = f(a)
If the function does not require any inputs, then you can call the function with
empty parentheses, such as
h = @ones;
a = h()
Without the parentheses, the assignment creates another function handle.
a=h
Thus:
a is now @ones
Function handles are variables that you can pass to other functions.
Eg:
q = integral(f,0,1);
Anonymous function:
Syntax:
h = @(arglist)anonymous_function
eg:
sqr = @(n) n.^2;
x = sqr(3)
Referencing Package Members from Outside the Package:
Functions, classes, and other packages contained in a package are scoped to that
package. To reference any of the package members, prefix the package name to the
member name, separated by a dot. For example, the following statement creates an
instance of MyClass, which is contained in mypack package.
obj = mypack.MyClass;
Accessing Class Members Various Scenarios:
This section shows you how to access various package members from outside a
package. Suppose that you have a package mypack with the following contents:
+mypack
+mypack/myfcn.m
+mypack/@MyFirstClass
+mypack/@MyFirstClass/myFcn.m
+mypack/@mMyFirstClass/otherFcn.m
+mypack/@MyFirstClass/MyFirstClass.m
+mypack/@MySecondClass
+mypack/@MySecondClass/MySecondClass.m
+mypack/+mysubpack
+mypack/+mysubpack/myFcn.m
Invoke the myFcn function in mypack:
mypack.myFcn(arg)

Create an instance of each class in mypack:


obj1 = mypack.MyFirstClass;
obj2 = mypack.MySecondClass(arg);
Invoke the myFcn function that is in the package mysubpack:
mypack.mysubpack.myFcn(arg1,arg2);
If mypack.MyFirstClass has a method called myFcn, call it like any method call on an
object:
obj = mypack.MyFirstClass;
myFcn(obj,arg);
If mypack.MyFirstClass has a property called MyProp, assign it using dot notation
and the object:
obj = mypack.MyFirstClass;
obj.MyProp = x;

Setting optimizsation set:


options = optimset('Display','iter','TolX',1e-8)
Description of Matlab function used in optimization:
x = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options) minimizes with the
optimization options specified in options. Use optimoptions to set these options. If
there are no nonlinear inequality or equality constraints, set nonlcon = [].

Matlab to Java Code Conversion:


Matlab code color:
Java code color:

Matlab Code
Abstract class or
interface in matlab

classdef PhaseProperties <


handle

Constant class
variables

properties (Constant)
PRESSURE_NC_PA =
101352.9322071000;
end
properties
%> String used to
identify the type of PLSeg.
type = 'pipe';

Changing instance
variables

Close equivalent Java


code
public abstract class
PhaseProperties
private static final double
PRESSURE_NC_PA =
101352.9322071000;
String type = "pipe";

Different Setters and


Getters

end
If no access is specified, the
default access specifier is
public.
properties (SetAccess =
private, GetAccess = public)
densityCorrectingFactor =
1;
end

Abstract methods

methods (Abstract)
[mySigmaNM,
mySigmaStdNM, errorId,
errorString] =
computesigma(MyPhasePro
perties, pressurePa,
temperatureK)
end

int
densityCorrectingFactor
= 1;
private void
setDensityCorrectingFact
or(int
densityCorrectingFactor)
{
this.densityCorrectingFac
tor =
densityCorrectingFactor;
}
Public void
getDensityCorrectingFact
or() {
return
this.densityCorrectingFac
tor;
}
public abstract
Map<String, Object>
computesigma(PhaseProp
erties MyPhaseProperties,
double pressurePa,
double temperatureK);
and the returned map will
have the required output
values in matlab i.e
mySigmaNM, mySigmaStdNM,
errorId, errorString

Constructor

Single line comment


Block comment

Methods / Functions
Multiple inheritance

function MyPhaseProperties
= PhaseProperties()
% Do nothing
end
% Commented code
%{ This
Is
Block
Comment
}%

PhaseProperties() {
// Do nothing
}

PLSeg < PhaseProperties &


matlab.mixin.Heterogeneou

Not supported in java

// Commented code
/* This
Is
Block
Comment
*/

Conversion of MATLAB Types to Java Types


Java Parameter Type (Scalar or Array)
MATLAB Argument
Closest Type <> Least Close Type

logical

boolean

byte

short

int

long

float

double

double

double

float

long

int

short

byte

boolean

single

float

double

char (1-by-1 scalar)

String

char

char (1-by-n or n-by-1,


n>1)

String

char[]

char(m-by-n, m,n>1)

String[]

uint8
int8

byte

short

int

long

float

double

uint16
int16

short

int

long

float

double

uint32
int32

int

long

float

double

uint64
int64

long

float

double

cell array of character vectors

String[]

Java object

Object

cell array of object

Object[]

MATLAB object

Unsupported

Passing Java Objects


MATLAB Argument

Java Object

logical

Boolean

double

Double

single

Float

char (1-by-1 scalar)

Character

char (1-by-n or n-by-1, n>1)

String

uint8
int8

Byte

MATLAB Argument

Java Object

uint16
int16

Short

uint32
int32

Integer

uint64
int64

Long

cell array of character vectors

String[]

Java object

Object

cell array of object

Object[]

MATLAB object

Unsupported

Ref below links for more detailed understanding:


http://www.mathworks.com/help/matlab/matlab_external/passing-data-to-a-java-method.html
http://www.mathworks.com/help/matlab/matlab_oop/property-attributes.html
http://www.mathworks.com/help/matlab/object-oriented-programming-in-matlab.html
http://www.mathworks.com/help/matlab/matlab_oop/developing-classes--typical-workflow.html
http://www.mathworks.com/help/matlab/matlab_oop/comparing-handle-and-value-classes.html
http://www.mathworks.com/help/matlab/matlab_oop/create-a-simple-class.html
http://www.mathworks.com/help/matlab/matlab_oop/example--representing-structured-data.html
http://www.mathworks.com/help/matlab/matlab_oop/using-objects-to-write-data-to-a-file.html
http://www.mathworks.com/help/matlab/matlab_oop/example--implementing-linked-lists.html

You might also like