Você está na página 1de 19

FreeMat Script/Matrix Manipulation Language

http://freemat.sourceforge.net/
FreeMat is a free environment for rapid engineering and scientific prototyping and data processing. It is similar to commercial systems such as MATLAB from Mathworks, and IDL from Research Systems, but is Open Source. FreeMat is available under the GPL license.

Variables in FreeMat
'cell' for cell-arrays 'struct' for structure-arrays 'logical' for logical arrays 'uint8' for unsigned 8-bit integers 'int8' for signed 8-bit integers 'uint16' for unsigned 16-bit integers 'int16' for signed 16-bit integers 'uint32' for unsigned 32-bit integers 'int32' for signed 32-bit integers 'uint64' for unsigned 64-bit integers 'int64' for signed 64-bit integers 'float' for 32-bit floating point numbers 'single' is a synonym for 'float' 'double' for 64-bit floating point numbers 'char' for string arrays

Example:
A = int8(1) -> A = 1 A = int8(-1) -> A = -11 A = uint8(1) -> A = 1 A = uint8(-1) -> A = 0 B = thisisastring

Type Conversion Functions in FreeMat


int2bin INT2BIN Convert Integer Arrays to Binary bin2int BIN2INT Convert Binary Arrays to Integer logical LOGICAL Convert to Logical string STRING Convert Array to String uint8 UINT8 Convert to Unsigned 8-bit Integer uint16 UINT16 Convert to Unsigned 16-bit Integer uint32 UINT32 Convert to Unsigned 32-bit Integer uint64 UINT64 Convert to Unsigned 64-bit Integer int8 INT8 Convert to Signed 8-bit Integer int16 INT16 Convert to Signed 16-bit Integer int32 INT32 Convert to Signed 32-bit Integer int64 INT64 Convert to Signed 64-bit Integer single SINGLE Convert to 32-bit Floating Point float FLOAT Convert to 32-bit Floating Point double DOUBLE Convert to 64-bit Floating Point complex COMPLEX Create a Complex Number dcomplex DCOMPLEX Convert to Double Precision (deprecated) cast CAST Typecast Variable to Specified Type char CHAR Convert to character array or string bin2dec BIN2DEC Convert Binary String to Decimal dec2bin DEC2BIN Convert Decimal to Binary String
Example:

y = cast(x, toclass)
'cell' for cell-arrays 'struct' for structure-arrays 'logical' for logical arrays 'uint8' for unsigned 8-bit integers 'int8' for signed 8-bit integers 'uint16' for unsigned 16-bit integers 'int16' for signed 16-bit integers 'uint32' for unsigned 32-bit integers 'int32' for signed 32-bit integers 'uint64' for unsigned 64-bit integers 'int64' for signed 64-bit integers 'float' for 32-bit floating point numbers 'single' is a synonym for 'float' 'double' for 64-bit floating point numbers 'char' for string arrays

Matrices in FreeMat
Matrices (Syntax)
variable = *row1; row2; row3; ; rowN] where each row contains the [column ]elements row1 = col1, col2, col3, , colN Example: Y = [1,2 ,3; 4, 5, 6] Ans = 1 2 3 4 5 6 There are also predefined like the following: zeros(N) diag(1:N) ones(N) rand(N)

Structures & Functions in FreeMat


Structures (Syntax)
variable = struct(field1, value1, field2, value2, , fieldN, valueN) Example: Y = struct(name,{jo,me}, age, {11,20}, section, phys168);
->Y(1) ans = name: jo age: 11 section: phys168 ->Y(2) ans = name: me age: 20 section: phys168 ->Y(1).name ans = jo TO ADD data: ->Y(3).name = you ->Y(3).age = 21 ->Y(3).section = phys168 ->Y(3) ans = name: you age: 21 section: phys168

Structures & Functions in FreeMat


Functions (Syntax)
function variable = fname(input1, input2, , inputN) content
Example: function y = series(n,x)

function *var1, var2, , varN] = fname(input1, input2, , inputN) content


Example: function [y,z] = series(n,x)

function fname(input1, input2, , inputN) content


Example: function series(n,x)

function *var1, var2, , varN] = fname content


Example: function y = series

Scripts in FreeMat
Sequence of commands contained in a .m file
Sample: test.m A = 3; printf(value of a is %f, A);

The output in the terminal will be, if executed: -> test value of a is 3 In running scripts, just type the name of the file in the console, Just like in the example, [ test ] WARNING: in your freemat console, current directory should be where the file is. Otherwise it will not run or detect the script.

Functions as Scripts in FreeMat


Functions can be placed inside a .m file
Sample: addtwo.m function sum = addtwovalues(A, B); sum = 0; sum = A + B; The output in the terminal will be, if executed: -> addtwo(1, 2) ans = 3 In running functions in scripts, just type the name of the file in the console, Just like in the example, [ addtwo(1, 2) ] WARNING: in your freemat console, current directory should be where the file is. Otherwise it will not run or detect the script.

Colon operator : in FreeMat


Distinct syntaxes for the colon : operator
variable = a : c variable = a : b : c The output will be: variable = [a, a+b, a+ 2b, ..., a+nb], where a+nb <= c Example: -> x = 1:2 x= 1 2 -> x = 5: -1 : 1 x= 5 4 3 2 1 -> x = 1:2:5 x= 1 3 5 -> x = 5:4 x= Empty array 1x0

Comparison operators in FreeMat


A total of 6 comparison operators
y y y y = = = = a a a a < b <= b > b >= b y = a ~= b (~ tilde. NOT EQUAL?) y = a == b (EQUAL?)

Modes of operation:

a is a scalar, b is an n-dimensional array - the output is then the same size as b, and contains the result of comparing each element in b to the scalar a. a is an n-dimensional array, b is a scalar - the output is the same size as a, and contains the result of comparing each element in a to the scalar b. a and b are both n-dimensional arrays of the same size - the output is then the same size as both a and b, and contains the result of an element-wise comparison between a and b.

Mathematical Operators in FreeMat


Add + Minus Leftdivide \ Rightdivide / Times * Power ^ Dotleftdivide .\ Dotrightdivide ./ Dotpower .^ Dottimes .* Hermitian conjugate transpose . Matrix Inverse inv

(for scalars& SquareMatrices only) (element-wise division) (element-wise division) (element-wise power) (element-wise multiplication) (apostrophe), conjugating transpose (simple transpose) (inverse a matrix)

Logical Operators in FreeMat


There are 3 logical operators Y = ~x not Y = a & b and Y = a | b or Freemat support shortcut evaluations, for example: If (expr1 & expr2) = if expr1 is false, expr2 is not evaluated at all If (expr1 | expr2) = if expr1 is true, expr2 is not evaluated at all

FOR Loops in FreeMat


Syntax: for ( variable = expression ) statements end for variable = expression statements end for variable statements end
Example: summ = 0; for (I = 1:100) summ = summ + i; end Example: summ = 0; for I = 1:100 summ = summ + i; end

% where variable is a vector


Example: summ = 0; i = 1:10:100; for i summ = summ + i; end

WHILE Loops in FreeMat


Syntax: while (test_expression) statements end
Example: summ = 0; x = 1; while (x <= 100) summ = summ + x; x = x + 1; end

IF-ELSEIF-ELSE statement in FreeMat


Syntax: if condition1 statement 1 elseif condition2 statement 2 else statement N end
Example: if(a == 2) var = two; elsif (a==3) var = three; else var = NAN; end

SWITCH statement in FreeMat


Syntax: switch (expression) case test1 statements case test2 statements otherwise statements end
Example: Function c = switch_test(a) switch(a) case {red, blue} c = color; case {rice, soup} c = food; otherwise c = anything else; end

TRY-CATCH statement in FreeMat


Used for error handling and control Syntax: try statements_1 catch statement2 end
Example: Readfile.m Function c = read_file(filename) try fp = fopen(filename, r) c = fgetline(fp); fclose(fp); catch c = [could not open, lasterr] end

In this way, freemat will try to execute first the statements_1 and if there will be errors, the statements inside the catch will be executed

FreeMat Functions
Some built-in functions clc clears the screen/console clear all clears used/declared variables,functions clock current date and time y = clock %this will create an output y = [year month day hour minute seconds] y(0) = 2013 etime elapsed time, same as clock format tic starts the stopwatch timer toc stops the stopwatch timer

Você também pode gostar