MATLAB facts for kids
Paradigm | multi-paradigm: functional, imperative, procedural, object-oriented, array |
---|---|
Designed by | Cleve Moler |
Developer | MathWorks |
First appeared | late 1970s |
Stable release | |
Typing discipline | dynamic, weak |
Filename extensions | .m, .p, .mex*, .mat, .fig, .mlx, .mlapp, .mltbx, .mlappinstall, .mlpkginstall |
Major implementations | |
MATLAB Software, GNU Octave, Sysquake | |
Influenced by | |
|
|
Influenced | |
|
![]() L-shaped membrane logo
|
|
Developer(s) | MathWorks |
---|---|
Initial release | 1984 |
Stable release | |
Written in | C/C++, MATLAB |
Operating system | Windows, macOS, and Linux |
Platform | IA-32, x86-64, ARM64 |
Type | Numerical computing |
License | Proprietary commercial software |
MATLAB (which stands for "MATrix LABoratory") is a special programming language and a computer program created by a company called MathWorks. It's used for doing lots of math, especially with numbers.
MATLAB helps you work with matrices (which are like grids of numbers), create graphs and charts from data, build algorithms (step-by-step instructions for computers), and even design user interfaces (the parts of a program you see and click). You can also connect it with programs written in other computer languages.
Even though MATLAB is mostly for number-based math, it has an extra tool that lets it do symbolic computing. This means it can work with math problems using symbols and letters, not just numbers. There's also another package called Simulink that helps design and test complex systems, like those found in cars or robots.
As of 2020, over four million people around the world use MATLAB. These users come from many different fields, like engineering, science, and economics. In 2017, more than 5,000 colleges and universities used MATLAB for teaching and research.
Contents
History of MATLAB
How MATLAB Started
MATLAB was created by a mathematician and computer programmer named Cleve Moler. He got the idea for it from his PhD studies in the 1960s. Moler became a math professor and started making MATLAB as a hobby to help his students. He first developed the math parts of MATLAB in 1967.
The very first version of MATLAB was finished in the late 1970s. It was shown to the public for the first time in 1979. These early versions were simple "matrix calculators" with 71 built-in math tools. Back then, MATLAB was given away for free to universities. Moler would leave copies at universities he visited, and it became very popular in university math departments.
In the 1980s, Cleve Moler met John N. Little. They decided to rewrite MATLAB in a different computer language called C. They wanted to sell it for the new IBM desktop computers that were becoming popular. John Little and another programmer, Steve Bangert, rewrote MATLAB and added new features, including "toolboxes" (collections of specialized functions).
Since 1993, there have been free alternatives to MATLAB, like GNU Octave and Scilab, which work in similar ways.
MATLAB as a Business
MATLAB was first sold as a product in 1984. The company MathWorks was started to develop and sell the software. The first sale happened the next year when a professor from Massachusetts Institute of Technology bought ten copies.
By the end of the 1980s, hundreds of copies of MATLAB were sold to universities. The software became very popular because of the toolboxes. These toolboxes were created by experts in different areas to do specific math tasks. Many students who used MATLAB at Stanford later took the software with them when they started working in companies.
Over time, MATLAB was updated to work on different computer systems. Version 3 came out in 1987. The first program that could turn MATLAB code into faster computer code was made in the 1990s.
In 2000, MathWorks updated MATLAB to use a faster math library. In 2004, they released a tool for parallel computing, which means MATLAB could use multiple computer processors at once to solve problems faster. In 2010, it gained the ability to use graphics cards (GPUs) for even more speed.
Recent Updates
Big changes were made to MATLAB in 2012 with version 8. The way users interacted with the program was updated, and Simulink got more features. By 2016, MATLAB had many technical and user-friendly improvements, including a new "Live Editor" that makes writing code easier.
How MATLAB Works (Syntax)
The MATLAB program is built around its own programming language. You can use MATLAB by typing commands directly into its "Command Window" or by running text files that contain MATLAB code.
"Hello, world!" Example
Here's a simple program that just says "Hello, world!" It's a classic first program in many languages.
disp('Hello, world!')
When you run this code, it will show:
Hello, world!
Variables in MATLAB
In MATLAB, you create variables (which are like containers for information) using the equals sign (`=`). MATLAB is a "weakly typed" language, meaning it can often figure out what kind of data you're storing without you having to tell it. It's also "inferred typed," so you don't always need to declare the type of a variable, and its type can even change.
For example:
>> x = 17
x =
17
>> x = 'hat'
x =
hat
>> x = [3*4, pi/2]
x =
12.0000 1.5708
>> y = 3*sin(x)
y =
-1.6097 3.0000
Vectors and Matrices
A simple list of numbers (called an array or vector) can be made using colons. For example:
>> array = 1:2:9
array =
1 3 5 7 9
This creates an array that starts at 1, adds 2 each time, and stops before going over 9. So, it gives you 1, 3, 5, 7, and 9.
If you don't put the middle number, MATLAB will add 1 each time by default:
>> ari = 1:5
ari =
1 2 3 4 5
This creates an array with 1, 2, 3, 4, and 5.
When you want to find an item in a list or grid, MATLAB starts counting from 1, not 0. This is different from some other programming languages.
You can make a grid of numbers (called a matrix) by putting numbers in square brackets `[]`. You separate numbers in a row with a space or comma, and you use a semicolon `;` to start a new row. To get a specific number from the matrix, you use parentheses `()`.
>> A = [16, 3, 2, 13 ; 5, 10, 11, 8 ; 9, 6, 7, 12 ; 4, 15, 14, 1]
A =
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
>> A(2,3)
ans =
11
The command `A(2,3)` asks for the number in the 2nd row and 3rd column, which is 11.
You can also select a whole section of a matrix. For example, `2:4` means "from 2 to 4".
>> A(2:4,3:4)
ans =
11 8
7 12
14 1
This selects the numbers from rows 2 through 4 and columns 3 through 4.
MATLAB has special commands to make common matrices:
- `eye(n,n)` makes an "identity matrix" (a square matrix with 1s on the main diagonal and 0s everywhere else).
- `zeros(rows,cols)` makes a matrix full of zeros.
- `ones(rows,cols)` makes a matrix full of ones.
>> eye(3,3)
ans =
1 0 0
0 1 0
0 0 1
>> zeros(2,3)
ans =
0 0 0
0 0 0
>> ones(2,3)
ans =
1 1 1
1 1 1
To flip a vector or matrix (turn rows into columns and columns into rows), you can use `transpose` or add a dot-prime `.'` after it.
>> A = [1 ; 2], B = A.', C = transpose(A)
A =
1
2
B =
1 2
C =
1 2
>> D = [0, 3 ; 1, 5], D.'
D =
0 3
1 5
ans =
0 1
3 5
Many MATLAB functions can work on entire arrays at once. For example, `mod(2*J,n)` will multiply every number in `J` by 2, then find the remainder when divided by `n`. While MATLAB has `for` and `while` loops, it's often faster to use these "vectorized" operations.
Structures
MATLAB can store data in "structures," which are like records that hold different types of information under specific names (called "fields"). Since all variables in MATLAB are arrays, these are called "structure arrays." Each item in the array has the same field names.
Functions
When you create your own MATLAB function, the file name should be the same as the function's name. Function names must start with a letter and can include letters, numbers, or underscores. MATLAB cares about whether letters are uppercase or lowercase.
Here's an example of a function that processes an image:
rgbImage = imread('ecg.png');
grayImage = rgb2gray(rgbImage); % This turns a color image into grayscale.
level = graythresh(grayImage); % This finds a good threshold to make the image black and white.
binaryImage = im2bw(grayImage, level);
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Make the black parts pure red.
redChannel(~binaryImage) = 255;
greenChannel(~binaryImage) = 0;
blueChannel(~binaryImage) = 0;
% Now recombine to form the output image.
rgbImageOut = cat(3, redChannel, greenChannel, blueChannel);
imshow(rgbImageOut);
Function Handles
MATLAB lets you create "function handles," which are like pointers to functions. You can use them to pass functions around as if they were variables.
Classes and Object-Oriented Programming
MATLAB supports object-oriented programming (OOP). This is a way of organizing code into "objects" that combine data and the actions that can be performed on that data. MATLAB has "classes" (blueprints for objects) and "inheritance" (where one class can get features from another).
Here's a simple example of a class:
classdef Hello
methods
function greet(obj)
disp('Hello!')
end
end
end
If you save this code in a file named `hello.m`, you can run it like this:
>> x = Hello();
>> x.greet();
Hello!
Graphics and User Interface Design
<graph>{ "version": 2, "width": 400, "height": 200, "data": [ { "name": "table", "values": [ { "x": 3, "y": 1 }, { "x": 1, "y": 3 }, { "x": 2, "y": 2 }, { "x": 3, "y": 4 } ] } ], "scales": [ { "name": "x", "type": "ordinal", "range": "width", "zero": false, "domain": { "data": "table", "field": "x" } }, { "name": "y", "type": "linear", "range": "height", "nice": true, "domain": { "data": "table", "field": "y" } } ], "axes": [ { "type": "x", "scale": "x" }, { "type": "y", "scale": "y" } ], "marks": [ { "type": "rect", "from": { "data": "table" }, "properties": { "enter": { "x": { "scale": "x", "field": "x" }, "y": { "scale": "y", "field": "y" }, "y2": { "scale": "y", "value": 0 }, "fill": { "value": "steelblue" }, "width": { "scale": "x", "band": "true", "offset": -1 } } } } ] }</graph>
MATLAB has great tools for making graphs and plots. For example, the `plot` function can draw a graph using two lists of numbers, `x` and `y`. The code below:
x = 0:pi/100:2*pi;
y = sin(x);
plot(x,y)
will create a graph of the sine function:
MATLAB can also create amazing three-dimensional (3D) graphics:
[X,Y] = meshgrid(-10:0.25:10,-10:0.25:10);
f = sinc(sqrt((X/pi).^2+(Y/pi).^2));
mesh(X,Y,f);
axis([-10 10 -10 10 -0.3 1])
xlabel('{\bfx}')
ylabel('{\bfy}')
zlabel('{\bfsinc} ({\bfR})')
hidden off |
[X,Y] = meshgrid(-10:0.25:10,-10:0.25:10);
f = sinc(sqrt((X/pi).^2+(Y/pi).^2));
surf(X,Y,f);
axis([-10 10 -10 10 -0.3 1])
xlabel('{\bfx}')
ylabel('{\bfy}')
zlabel('{\bfsinc} ({\bfR})') |
|
This code makes a wireframe 3D plot of a special math function: | This code makes a surface 3D plot of the same math function: | |
MATLAB also lets you create graphical user interface (GUI) applications. These are programs with buttons, menus, and other visual elements that you can click and interact with. You can design them using special tools like GUIDE and App Designer.
MATLAB and Other Languages
MATLAB can work with code written in other programming languages like C or Fortran. This means you can use existing code from those languages within your MATLAB projects. Since 2014, it has also been able to connect more easily with Python.
You can also use libraries (collections of pre-written code) from languages like Perl, Java, or .NET directly in MATLAB. Many MATLAB tools, like those for handling XML files or SQL databases, are built using these other language libraries.
For symbolic math, MATLAB can connect to other powerful math programs like Maple or Mathematica.
See also
In Spanish: MATLAB para niños