Working with Objects in MATLAB - MATLAB & Simulink (2024)

Open Live Script

Some MATLAB® functions return objects. Objects combine data (properties) with functions and methods. Object properties contain data, including simple types like numbers or text, or other objects. The functions and methods perform actions on the objects themselves. These functions can act on the object properties or change the state of the object, for example.

Create an Object

When creating an object, you can assign a variable to that object. The variable provides access to the properties and methods of the object. For example, this syntax for the histogram function not only displays a histogram of the data in x but also returns the object as the output h.

h = histogram(x)

Create a histogram object that displays 1000 random numbers. Calling histogram with an output argument displays the graph, the type or class of the object (Histogram), and a partial list of the object properties and their values.

x = randn(1000,1);h = histogram(x)

Working with Objects in MATLAB- MATLAB & Simulink (1)

h = Histogram with properties: Data: [1000x1 double] Values: [3 1 2 15 17 27 53 79 85 101 127 110 124 95 67 32 27 16 6 6 4 1 2] NumBins: 23 BinEdges: [-3.3000 -3.0000 -2.7000 -2.4000 -2.1000 -1.8000 -1.5000 -1.2000 -0.9000 -0.6000 -0.3000 0 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000 2.1000 2.4000 2.7000 3 3.3000 3.6000] BinWidth: 0.3000 BinLimits: [-3.3000 3.6000] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0 0 0] Use GET to show all properties

In the workspace, the histogram object is listed with the other active variables, including the dimensions and the type of object.

Working with Objects in MATLAB- MATLAB & Simulink (2)

Get and Set Object Properties

Object properties contain data. By changing property values, you can modify certain aspects of an object. You can use dot notation with the variable assigned to the object to access and change its properties.

In the case of a histogram object, properties contain the raw data, the number of bins, the height of each bar, and other information that controls the appearance of the histogram. For example, Orientation is a property of histogram objects that determines whether the bars are displayed horizontally or vertically. You can access the value of the property by entering the name of the object (h), a dot, and the property name.

h.Orientation
ans = 'vertical'

You can change the value of the property using the same syntax, but with the addition of an equal sign and the new value.

h.Orientation = "horizontal"

Working with Objects in MATLAB- MATLAB & Simulink (3)

h = Histogram with properties: Data: [1000x1 double] Values: [3 1 2 15 17 27 53 79 85 101 127 110 124 95 67 32 27 16 6 6 4 1 2] NumBins: 23 BinEdges: [-3.3000 -3.0000 -2.7000 -2.4000 -2.1000 -1.8000 -1.5000 -1.2000 -0.9000 -0.6000 -0.3000 0 0.3000 0.6000 0.9000 1.2000 1.5000 1.8000 2.1000 2.4000 2.7000 3 3.3000 3.6000] BinWidth: 0.3000 BinLimits: [-3.3000 3.6000] Normalization: 'count' FaceColor: 'auto' EdgeColor: [0 0 0] Use GET to show all properties

Not all object properties are writeable. A property can be read-only, meaning that it can be read, but trying to assign a new value to it returns an error. For example, the Values property of a histogram stores the height of each bar and is calculated when the object is created. It is a read-only property, so you cannot change Values directly.

Functions That Accept Objects

Some functions are designed to perform actions on an object. These functions can be methods, which are defined specifically for one class of objects, or can be functions that accept that object as an ordinary input argument. In either case, you can use the variable for the object as an input. For example, Histogram objects include the functions morebins and fewerbins, which increase and decrease the number of histogram bins, respectively. Access the NumBins property to see how many bins are currently in the histogram.

h.NumBins
ans = 23

Call morebins to increase the number of bins in histogram h. The morebins function increases the number of bins by approximately 10%, so the number of bins increases from 23 to 26.

morebins(h)

Working with Objects in MATLAB- MATLAB & Simulink (4)

ans = 26

Other object-oriented programming languages frequently use dot notation to call methods, such as h.morebins. That syntax also is supported for methods in MATLAB. However, for consistency in sample code, the documentation uses function form for most methods and functions that accept object inputs.

The morebins function does not have any additional input arguments, but that is not true of all functions that accept objects. Besides the object itself, object functions can have additional input arguments, which you can pass to the function with standard function syntax. For example, you can call to an object function that takes two input arguments with this syntax:

functionName(objectVariable,arg1,arg2)

Define Your Own Class-Based Objects

In addition to the objects provided in the MATLAB language, you can define your own class-based objects using object-oriented programming techniques. The language follows standard OO conventions. For more information, start with Creating a Simple Class.

See Also

Fundamental MATLAB Classes | Creating a Simple Class

Working with Objects in MATLAB
- MATLAB & Simulink (2024)
Top Articles
Maths on the Playground: Engaging Children with Numbers Through Play - LearningMole
Pedal and Calculate: The Math Playground Mountain Bike Racer Experience
Google Jobs Denver
Brett Cooper Wikifeet
Palmbeachschools Jobs
Tc-656 Utah
Superhot Unblocked Games
Sofia Pinkman
Basic Setup – OpenXR & Pimax HMDs...
Edgenuity Answer Key Algebra 1 Pdf
Press And Sun-Bulletin Obits Today
Shaw Centre for the Salish Sea — Eight Arms, Eight Interesting Facts: World Octopus Day
Real Estate Transfers Erie Pa
5 Best Brokerage Accounts for High Interest Rates on Cash Sweep - NerdWallet
‘An affront to the memories of British sailors’: the lies that sank Hollywood’s sub thriller U-571
All classes in Pathfinder: Wrath of the Righteous
Nyu Paralegal Program
Big Lots $99 Fireplace
My Sagu Blackboard
Ma.speedtest.rcn/Merlin
Free Bubble Letters Generator | Add bubble letters with a click!
Linus Tech Tips Forums
Open jazz : podcast et émission en replay | France Musique
Isaimini 2023: Tamil Movies Download HD Hollywood
Paige Van Zant Of Leak
Acb Message Board Yahoo
Wolf Of Wall Street Tamil Dubbed Full Movie
Greet In Cheshire Crossword Clue
Management Trainee: Associate Adjuster - June 2025
Walgreens On Nacogdoches And O'connor
Wells Fargo Hiring Hundreds to Develop New Tech Hub in the Columbus Region
Chihuahua Adoption in Las Vegas, NV: Chihuahua Puppies for Sale in Las Vegas, NV - Adoptapet.com
Adding Performance to Harley Davidson & Motorcycles is Easy with K&N
Stellaris How To Get Subjugation Casus Belli
Unblocked Games 76 Bitlife
Lubbock, Texas hotels, motels: rates, availability
Brooklyn Park City Hall
Cititrends Workday Login
Green Press Gazette Obits
Fineassarri
Rubmd.com.louisville
Petra Gorski Obituary
Busted Newspaper Mcpherson Kansas
Katopunk Pegging
Craigs List Williamsport
Roman Numerals Chart, Translation Tips & History
Noel Berry's Biography: Age, Height, Boyfriend, Family, Net Worth
SF bay area cars & trucks "chevrolet 50" - craigslist
Knock At The Cabin Showtimes Near Alamo Drafthouse Raleigh
The Complete History Of The Yahoo Logo - Hatchwise
Fitgirl Starfield
Jaggers Nutrition Menu
Latest Posts
Article information

Author: Rubie Ullrich

Last Updated:

Views: 6336

Rating: 4.1 / 5 (72 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Rubie Ullrich

Birthday: 1998-02-02

Address: 743 Stoltenberg Center, Genovevaville, NJ 59925-3119

Phone: +2202978377583

Job: Administration Engineer

Hobby: Surfing, Sailing, Listening to music, Web surfing, Kitesurfing, Geocaching, Backpacking

Introduction: My name is Rubie Ullrich, I am a enthusiastic, perfect, tender, vivacious, talented, famous, delightful person who loves writing and wants to share my knowledge and understanding with you.