Top Banner
1 © 2014 The MathWorks, Inc. MATLAB to iPhone Made Easy Generating readable and portable C code from your MATLAB algorithms for your iPhone or iPad app Bill Chou
18

MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

May 20, 2018

Download

Documents

hakhue
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

1© 2014 The MathWorks, Inc.

MATLAB to iPhone Made Easy

Generating readable and portable C code from your MATLAB algorithmsfor your iPhone or iPad app

Bill Chou

Page 2: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

2

Page 3: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

4

Page 4: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

5

Quick DemoMATLAB Coder

>> Demo

Page 5: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

6

Agenda Using MATLAB Coder

– Challenges of manual translation– Two-step workflow for generating code

Integrating Generated Code into iOS App

Page 6: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

7

Challenges with Manual Translationfrom MATLAB to C to iOS

Separate functional and implementation specification– Leads to multiple implementations that are inconsistent– Hard to modify requirements during development– Difficult to keep reference MATLAB code and C code in sync

Manual coding errors Time-consuming and expensive process

Re-code in CAlgorithm Designin MATLAB

V1 V1

itera

te

V2V3 V2

V3ite

rate

Iterate, test, verify

Page 7: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

8

Automatic Translationfrom MATLAB to C to iOS

Algorithm Designand Code Generation

in MATLAB

.c

itera

te

With MATLAB Coder, design engineers can:

• Maintain one design in MATLAB • Design faster and get to C quickly• Test more systematically and frequently • Spend more time improving algorithms in MATLAB

Page 8: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

9

Simple Demo c = a*b

Code generation readiness tool Autodefine input type Code generation report

>> Demo

Page 9: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

10

Using MATLAB Coder: Two-Step Workflow

Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation choices Use supported language features

Iterate to Optimize generated C Code Test and verify generated C code matches behavior of MATLAB code Iterate MATLAB code to optimize

Assess Code Readiness

Update MATLAB Code

Prepare MATLAB Code

Test, Generate,

Verify C Code

Update MATLAB Code

Iterate to Optimize C Code

Page 10: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

11

Implementation Considerations

double foo(double b, double c){

return b*c;}

void foo(const double b[15], const double c[30], double a[18])

{int i0, i1, i2;for (i0 = 0; i0 < 3; i0++) {

for (i1 = 0; i1 < 6; i1++) {a[i0 + 3 * i1] = 0.0;for (i2 = 0; i2 < 5; i2++) {

a[i0 + 3 * i1] += b[i0 + 3 * i2] * c[i2 + 5 * i1];}

}}

}

function a= foo(b,c)a = b * c;

Element by element multiply

Dot product

Matrix multiply

Element by element multiply

Dot product

Matrix multiply

Element by element multiply

Dot product

Matrix multiply

logicalintegerrealcomplex…

C

Page 11: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

12

Implementation Considerations

Polymorphism Memory allocation Processing matrices and arrays Fixed-point data types

7 Lines of MATLAB105 Lines of C

Page 12: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

13

Example: Newton/Raphson Algorithm

Preparing your MATLAB code

Pre-allocate Identify more efficient constructs Select code generation options

>> Demo

Page 13: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

14

MATLAB Language Support for Code Generation

Java

visualization

graphics

nested functions

sparse

variable-sized data

arrays

structnumeric

fixed-point

functions

complexSystem objects

global

persistent

malloc

classescell arrays

Page 14: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

15

Supported MATLAB LanguageFeatures and Functions

Broad set of language features and functions/system objects supported for code generation

Matrices and Arrays Data Types Programming Constructs Functions

• Matrix operations• N-dimensional arrays• Subscripting• Frames• Persistent variables • Global variables

• Complex numbers• Integer math• Double/single-precision• Fixed-point arithmetic• Characters• Structures• Numeric class• Variable-sized data• MATLAB Class (MCOS)• System objects

• Arithmetic, relational, and logical operators

• Program control (if, for, while, switch)

• MATLAB functions and subfunctions• Variable-length argument lists• Function handles

Supported algorithms• More than 700 MATLAB operators and

functions• More than 300 System objects for:

• Signal processing• Communications • Computer vision

Page 15: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

16

Using MATLAB Coder– Challenges of manual translation– Two-step workflow for generating code

Integrating Generated Code into iOS App

Agenda

Page 16: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

17

Demo: Integrating Generated Code into iOS App

MATLAB Coder

>> Demo

Page 17: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

18

More Information

To learn more, visit the product page:mathworks.com/products/matlab-coder

To request a trial license: – Talk to your MathWorks account manager to request a trial license and set up a guided

evaluation with an application engineer

Page 18: MATLAB to iPhone Made Easy - MathWorks - Makers of ... Using MATLAB Coder: Two-Step Workflow Prepare your MATLAB algorithm for code generation Assess code readiness Make implementation

19

Q&A