You are on page 1of 17

Programming with MATLAB

2013 The MathWorks, Inc.1

Todays Objectives

Introduce you to programming with MATLAB


The MATLAB language
Development tools

Show you how to program


effectively in MATLAB

Demonstrate the range of


programming styles supported
Interactive command line,
scripts and functions,
object-oriented programming
2

Demo: Assessment of Wind Turbine Locations

Goal: Create analysis to choose best wind


turbine location by estimating
power generation at multiple locations

Approach:
Interactively explore data and develop
analysis approach

Load and preprocess data


from observation towers
Fit wind speed probability distribution
Estimate average power

Automate to run on data from


other locations
3

Calculating Average Turbine Power


( )

Wind Turbine Power Curve


Region I: > in
=0
Region II: in< < rated
=

2 2

2
2

Region III: rated< < out


=

Region IV: > out


=0
5

Demo Summary

Products Used
MATLAB
Statistics Toolbox

Started working interactively


Import Tool
Command line

Automated with a script


Command History
Comments and sections
Profiler to assess performance

Generalized with a function


to handle multiple files

Range of Programming Techniques


value
variable

structure

Refine and improve code


Maintainable
Reusable/ more general
Robust
function
script
command line

Refine and Improve Code

Maintainable
Data types (structures, cell arrays, objects)
Function types (subfunctions, nested functions,
private functions, packages)

Reusable / more general


Flexible calling syntax (varargin, varargout)

Robust
Error checking and validating inputs
(validateattributes, validatestring, error, inputParser)

Unit testing (matlab.unittest)

Development tools
Editor, Debugger, Code Analyzer, Profiler
8

Additional MATLAB Programming


Language Features

Object-oriented (OO) programming with MATLAB


Objects combine related data and algorithms
Class definition files describe object behavior
Use matrix-based aspects of MATLAB with objects

Includes events and listeners, reference (handle) semantics, etc.

Parallel programming with Parallel Computing Toolbox


Speeds up MATLAB applications on multicore desktops,
GPUs, and clusters
Handles large datasets by distributing data across
MATLAB workers
Provides levels of parallel programming constructs

Simple constructs (built in support for Toolboxes, parfor, gpuArray)


Advanced constructs (createJob/createTask, MPI, CUDA)

Sharing Results from MATLAB

Automatically generate reports

Create graphical user interfaces

Package as an app

Deploy applications to
other environments

Automatically generate
standalone C code
10

Summary MATLAB for Programming

High-level language
Matrix-based
Math and graphics functions
Traditional programming
language features

Interactive development environment


Tools, visualizations, and help

Supports a range of programming styles


Interactive command line,
scripts and functions,
object-oriented programming
11

Summary
The MATLAB Language

High-level language
Matrix-based
Supports a range of programming styles
No need for low-level administrative tasks

Math and graphics functions


for engineering and science

Includes features of traditional


programming languages
Flow control and error handling
Fundamental data types and
advanced data structures
12

Summary
MATLAB Development Environment

Command Window
Provides immediate results by
interactively executing commands

Data visualization

MATLAB Editor
Debugger
Code Analyzer

MATLAB Profiler

Additional tools for managing code and data


(e.g., shortcuts, code comparison, variable editor)
13

Summary
Multiple Ways to Get Help

doc

help <name>

Function Browser,
function hints,
tab completion

14

Resources

Webinars
Object-Oriented Programming in MATLAB
MATLAB for C/C++ Programmers
MATLAB to C Made Easy

Videos and code examples


MATLAB product page
Documentation

MATLAB Central
Open exchange for the MATLAB
and Simulink user community

15

MATLAB Central

File Exchange
Download free files
Over 10K available including functions,
apps, examples, and models

MATLAB Answers
Ask programming questions
Search 18K+ existing answers

Cody
Challenge and expand your
knowledge of MATLAB

Blogs
Read commentary from MathWorks engineers who
design, build, and support MATLAB and Simulink
16

2013 The MathWorks, Inc.


17

You might also like