Transcription
Introduction to Object-OrientedProgramming in MATLABJamie WinterSr. Account ManagerAbhishek GuptaApplication Engineer 2011 The MathWorks, Inc.1
Agenda Object-oriented programming Basic object-oriented programmingsyntax in MATLAB Classes in MATLAB2
What is a program?Datax 12while (x 100)x x 1if (x 23)disp('Hello')endendCodex 12while (x 100)x x 1if (x 23)disp('Hello')endendAssignmentLooping TestIncrementTest to ActTake ActionEndEndActions3
Progression of Programming TechniquesDatavaluevariablestructureLevel of Abstraction / Sophisticationfunctionscriptcommand lineAlgorithm4
Example: Sensor Array Transmitting a signalfrom a weather balloon Locating the signalwith a sensor array Computing the angle ofarrival (AoA) for the signal5
Procedural Programming Easy to learnMinimal planningThere is no formalrelationship betweendata and functions.Every detail is exposed.6
Data and Actions to ImplementWavelengthNumberSpacingLocationDetermine peaksCompute FFTPlot resultsReadingFrequencyDataSynthesize measurementsActions7
Related Data and ActionsBalloonWavelength LocationSignalLocation Frequency WavelengthFrequencyDataNumberSensorSpacing Reading Spacing ReadingNumberDetermine peaksSensor Synthesize measurementsComputeFFT peaks Determine Compute FFT Plot results Plot resultsSynthesize measurementsActions8
Grouping Related Data and ActionsBalloon LocationSignal Frequency WavelengthDataSensor Synthesize measurements Determine peaksSensor Synthesize measurements Compute FFTSensor Determine peaks Plot results Sensor Reading Compute FFT Reading Spacing Plot results Spacing Number NumberClassActions9
Progression of Programming vel of Abstraction / Sophisticationclass(methods)functionscriptcommand lineAlgorithm10
Object-Oriented Terminology Class– Outline of an idea– Properties (data)– Methods (algorithms) An element ofthe set – objectObject– Specific example of a class– InstanceDefined set – class11
Agenda Object-oriented programming Basic object-oriented programmingsyntax in MATLAB Classes in MATLAB12
Demonstration: Building a Simple Class Define a class forour radar blips Create the weatherballoon object Use the object inplace of the structure13
Objects Are easy to create Manage their own data Are interchangeablewith a structure– No other code changes are required.– Properties behave similar to field names.– Fields can’t be added arbitrarily.14
Demonstration:Adding Methods to a Class Start from a sensor classwith existing properties Add a method to computeangle of arrival (AoA) Integrate a sensor objectinto the existing codeSensorSynthesize measurementsDetermine peaksCompute FFTPlot results15
Objects with Methods Have immediate access totheir own data (properties) Allow you to overloadexisting functions Allow you to performcustom actions atcreation and deletion16
Agenda Object-oriented programming Basic object-oriented programmingsyntax in MATLAB Classes in MATLAB17
Taking Methods and Properties Further Control access Create constants Make values interdependent Execute methods when properties change18
Demonstration: Applying Attributes Control accessAccess publicAccess protected Restrict modificationConstantDependent19
EncapsulationSpeed ofLightNoise RatioNumber of Towersetc.SynthesizemeasurementsTower SpacingComputeFFTSensorSensor ReadingPlot ResultsDeterminePeaksCompute AoA20
Encapsulation Separates the interfacefrom the implementationSpeed ofLightof Towers NumberSimplifiesobject Noise RatiouseNumber of Towersetc.SynthesizemeasurementsBecomes a building blockTower SpacingSensor ReadingComputeFFTPlot ResultsDetermineTower SpacingPeaksSensorCompute AoA21
Using a Class as a Building BlockThe Red BaronThe BalloonAll Moving Radar BlipsAll Radar Blips22
Demonstration:Creating a Moving Target Define a new classfor moving blips Inherit from the existingclass for blips Add a methodPositionMove BlipSignal Use the moving blip23
Inheritance Subclass substitutesfor the superclass Allows re-envisioningand re-implementingthe superclass Builds on proven code Allows inheriting from the base MATLAB classes24
Object-Oriented Programming in MATLAB Class definition file describes object behaviorObjects can substitute for structuresApply attributes for a clean interfaceBuild on existing classes with inheritanceExtends the matrix-based language to objects25
Additional Resources26
Questions and Answers27
Programming in MATLAB Jamie Winter Sr. Account Manager Abhishek Gupta Application Engineer . 2 AgendaFile Size: 1MB