MATLAB Programming

From Wikibooks, open books for an open world
Jump to: navigation, search

MATLAB is a programming language developed by MathWorks. It started out as a matrix programming language where linear algebra programming was simple. It can be run both under interactive sessions and as a batch job.

Most MATLAB scripts and functions can be run in the open source programme octave. This is freely available for most computing platforms.

GNU Octave and LabVIEW MathScript are systems for numerical computations with an m-file script language that is mostly compatible with MATLAB. Both alternatives can replace MATLAB in many circumstances. While a good deal of the content of this book will also apply to both Octave and LabVIEW MathScript, it is not guaranteed to work in exactly the same manner. Differences and comparison between MATLAB and Octave are presented in Comparing Octave and MATLAB.

A Tutorial Introduction[edit]

Basic MATLAB Concepts[edit]

Saving and loading a MAT-file50%.svg[edit]

clc; clear all; close all;

[filename, pathname] = uigetfile( {'*.jpg';'*.bmp'}); aa=imread([pathname,filename]); I=aa; bb=imresize(aa,[256 256]); imshow(bb);title('input image');

%%...............%%% RGB2Lab color conversion %%%.........

rgbimage=makecform('srgb2lab'); labimage=applycform(bb,rgbimage); figure, imshow(labimage);title('color conversion image');

%%..............%%% seperate L* a* b* color space %%%.........

L=im2double(labimage(:,:,1)); a=im2double(labimage(:,:,2)); b=im2double(labimage(:,:,3)); figure,imshow(L);title('L* image'); [h,w,o]=size(L);

%%..........%%% applying filter in L* color space %%%..........

for s=1:3

   W(s)=h/2^s;
   for i=1:h
       for j=1:w
          p=(i-W(s));
          q=(i+W(s));
          r=(j-W(s));
          t=(j+W(s));
          if p<=0
             p=1;
          end
          if q>256
             q=256;
          end
          if r<=0
             r=1;
          end
          if t>256
             t=256;
          end
          m=p:q;
          n=r:t;
          %N(s)=W(s)*W(s);
          N(s)=size(m,2)*size(n,2);
          sum1=sum(sum(L(m,n)));
          V=1/N(s);
          A=V*sum1;
         
          B(i,j)=L(i,j)-A;                                                      
       end
   end
             
   %B=L-A;
   %figure,imshow(B);
   azx(:,:,s)=B;
  

end

contrast_L=azx(:,:,1)+azx(:,:,2)+azx(:,:,3); figure,imshow(contrast_L),title('contrast map L* image');

%%..............%%% find inverse of a* %%%............

figure,imshow(a);title('a* image'); d=max(max(a)); yy=d-a; yy=(yy*5); figure,imshow(yy);title('inv a* image'); [h,w,o]=size(yy);

%%.............%%% applying filter in a* color space %%%........

for s=1:3

   W(s)=h/2^s;
   for i=1:h
       for j=1:w
          p=(i-W(s));
          q=(i+W(s));
          r=(j-W(s));
          t=(j+W(s));
          if p<=0
             p=1;
          end
          if q>256
             q=256;
          end
          if r<=0
             r=1;
          end
          if t>256
             t=256;
          end
          m=p:q;
          n=r:t;
          %N(s)=W(s)*W(s);
          N(s)=size(m,2)*size(n,2);
          sum1=sum(sum(yy(m,n)));
          V=1/N(s);
          A=V*sum1;
         
          C(i,j)=yy(i,j)-A;                                                      
       end
   end
              
   %C=L-A;
   %figure,imshow(C);
   aza(:,:,s)=C;
  

end

contrast_a=aza(:,:,1)+aza(:,:,2)+aza(:,:,3); figure,imshow(contrast_a),title('contrast map a* image');

%%.............%%% find final scaling contrast map %%%.......

Scaling_contrast=imadd(contrast_L,contrast_a); figure,imshow(Scaling_contrast),title('Scaling contrast map');

%%...........%%% gabor filter outputs %%%.......................

[gabor_timage,u,v,GT,filter]=final_gabor(aa);

%%..........%%% removing erythema & dark pixels %%%..........

ts=0.4;  %% set threshold value R=Scaling_contrast; for i=1:h

   for j=1:w
       if Scaling_contrast(i,j)>=ts
           R(i,j)=1;
       else
           R(i,j)=0;
       end
   end

end

figure,imshow(R);title('Removing erythema');

The MATLAB Command Prompt50%.svg[edit]

  • Calculator

Data Storage and Manipulation[edit]

Data Types and Operators on Point Values[edit]

Arrays and Matrices[edit]

Graphics[edit]

Graphics25%.svg[edit]

Handle Graphics25%.svg[edit]

What is a handle?
Figure handles
Axis handles
Other types of handles

Annotating Plots25%.svg[edit]

M-file Programming[edit]

Scripts25%.svg[edit]

Control Flow75%.svg[edit]

Error Messages75%.svg[edit]

Debugging M Files50%.svg[edit]

Numerical Manipulation[edit]

Linear Algebra[edit]

It is the Matrix laboratory after all.

  • Operations
  • Transpose
  • Systems of linear equations
  • Row reduced echelon form
  • Inverse
  • Coffactor, minor
  • Jacobian

Differential Equations[edit]

More advanced I/O[edit]

Different versions of MATLAB handle this differently. We will focus on versions 6.5 and 7.x, primarily on MATLAB 7.x since it is the latest. A note will appear when the procedure is different for ver. 6.

Reading and writing from files[edit]

Writing and Reading to A Serial Port[edit]

Writing to a USB port[edit]

Examples[edit]

Filtering25%.svg[edit]

  • Moving Average
  • Alpha Beta
  • Kalman
  • PSD estimation
  • Entropy
  • Markov Processes
  • Queuing Theory

Controls25%.svg[edit]

Phase vocoder[edit]

See "Phase vocoder and encoder in MATLAB" for an example phase vocoder and the corresponding sound sample encoder in MATLAB.

Object Oriented Programming[edit]

MATLAB as well as Octave have object oriented capabilities. Yet, technically it is not fully an object oriented language.

An Object Oriented Language(OOL) has three components: 1. Polymorphism 2. Inheritance 3. Encapsulation

Octave can be extended by adding new objects. Those objects can overload the operators like e.g. assignment, slicing, comparison.

While in MATLAB, this can be done with m-script, in Octave new objects are implemented as C++ classes. A simple example of how objects can be added to Octave can be found here.

Struct arrays25%.svg[edit]

MATLAB classes00%.svg[edit]

An alternative to MATLAB: Octave[edit]

What is Octave ?[edit]

A short presentation of Octave and its history.

Differences between Octave and MATLAB[edit]

The most important differences between Octave and MATLAB that anyone willing to use Octave instead of MATLAB should be aware of.

Toolboxes and Extensions[edit]

The toolboxes are pretty good if you can afford them. In version 7 there are a lot of toolboxes.

Symbolic Toolbox75%.svg[edit]

Image Processing Toolbox00%.svg[edit]

MATLAB Compiler00%.svg[edit]

Legacy Toolboxes[edit]

  • GUIDE00%.svg allows the creation of interactive user interfaces.
  • Simulink00%.svg is for modeling, simulating and analysing systems.
  • Psychtoolbox00%.svg is a set of tools that aid in vision research.
  • Distributed computing- The distributed computing toolbox is a set of tools that aid in distributing models over a cluster.
  • Optimization- The optimization toolbox includes various algorithms for minimization.

MATLAB in medicine[edit]

„Image Processing in Optical Coherence Tomography using Matlab” is a book which will introduce you to subtleties related to the implementation of selected fragments of algorithms, the notation of some of them in the MATLAB environment has been given. The presented source code is shown only in the form of example of implementable selected algorithm. The book is addressed to ophthalmologists , IT specialists and students involved in the development of applications designed for automation of measurements for the needs of medicine.

References[edit]

External links[edit]

Other Wikibooks[edit]

A number of other wikibooks use MATLAB to teach their subjects. The following wikibooks make use of MATLAB:

Organization[edit]