Matlab

From XennisWiki
Jump to: navigation, search

MATLAB (matrix laboratory) is a multi-paradigm numerical computing environment and fourth-generation programming language. [...] MATLAB allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages, including C, C++, Java, Fortran and Python. (Wikipedia)

Installation

Debian

Further information: Ubuntu Community Help Wiki - MATLAB

  • Download Matlab from MathWorks (into your home directory) and unzip it
  • Start installation by ./install and follow the instructions
  • Add an entry to the launcher and other features by installing apt-get install matlab-support

Basics

Calculations

% Vektoren anlegen
a = [1 3 2 5 7 5 2]';
b = [3 2 1 3 4 5 6]';
% Winkel berechnen
cosAlpha = dot(a,b)/(norm(a)*norm(b))
alpha = acos(cosAlpha)
% 7x7 Einheitsmatrix
A = eye(7);
% Teilmatrix
B = A(5 : 7, 5 : 7)
% Matrix anlegen
B = [1 2 3; 4 5 6; 7 8 9]
A(5 : 7, 5 : 7) = B
% LGS lösen
x=A\a
y=A\b

Ausgabe

cosAlpha =
    0.8413

alpha =
    0.5711

B =
     1     0     0
     0     1     0
     0     0     1

B =
     1     2     3
     4     5     6
     7     8     9

A =
     1     0     0     0     0     0     0
     0     1     0     0     0     0     0
     0     0     1     0     0     0     0
     0     0     0     1     0     0     0
     0     0     0     0     1     2     3
     0     0     0     0     4     5     6
     0     0     0     0     7     8     9

x =
  1.0e+015 *

    0.0000
    0.0000
    0.0000
    0.0000
    4.5036
   -9.0072
    4.5036

y =
    3.0000
    2.0000
    1.0000
    3.0000
   -7.3333
   11.6667
   -4.0000

Comment

% Comment

%% Section

%{
Multiple line
comment
goes here
%}

For loop

for n = 1:3 % from 1 to 3, step 1
    n % 1, 2, 3
end

for n = 1:2:10 % from 1 to 20, step 2
    n % 1, 3, 5, ...
end

Condition

if exitflag ~= 1
    disp('if case')
else
    disp('else case')
end

Function

myFunction.m

function [out1, out2] = myFunction (in1, in2)
% myFunction - Just an example function
%
%   Example:   [out1, out2] = myFunction('Hello', 'World')
%
%   in1: A string
%   in2: Another string
%   return out1: The second input string
%   return out2: The first input string
%

    if nargin < 2
        error('Not enough input arguments.');
    end

    try
        % do something
    catch
        warning('This is a warning')
    end

    out1 = in2;
    out2 = in1;
end

Load .mat file

load('myData.mat');

Output text

disp('Output this text')

Weitere Befehle

Versions informationen (z.B. alle enthaltenden Toolboxen)

ver

Suchpfade

path

Pfad einer Methode

which NAME-DER-FUNKTION

Um alles zu schließen und zu bereinigen

clc
close all
clear all

Ordner zum Suchpfad hinzufügen, sodass die dort abgelegten Funktion und Scripte ebenfalls aufgerufen werden können

addpath('../pfad/irgendwo/hin')

Hilfreiches

In Konsole ausführen

Script exampleScript.m ausführen

matlab -nodisplay -nosplash -nodesktop -r exampleScript.m

Entspricht jeweils dem Aufruf exampleFunction(arg1)

matlab -nodisplay -nosplash -nodesktop -r "exampleFunction arg1; exit"
matlab -nodisplay -nosplash -nodesktop -r "exampleFunction(arg1); exit"

Script example.m in einem anderem Verzeichnis ausführen

matlab -nodisplay -nosplash -nodesktop -r "run path/example"

Figure als PDF ohne Rand speichern

siehe MATLAB: Figure als PDF speichern (ohne Rand!)

See also

External links