MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1
Symbol Symbol Name Role Description Examples

[ ] Square brackets


  • Array construction

  • Array concatenation

  • Empty matrix and array element deletion

  • Multiple output argument assignment


Square brackets enable array construction and concatenation, creation of empty matrices, deletion
of array elements, and capturing values returned by a function.


  • “Creating, Concatenating, and Expanding Matrices”

  • horzcat

  • vertcat


Construct a three-element vector:

X = [10 12 -3]

Add a new bottom row to a matrix:

A = rand(3);
A = [A; 10 20 30]

Create an empty matrix:

A = []

Delete a matrix column:

A(:,1) = []

Capture three output arguments from a function:

[C,iA,iB] = union(A,B)

{ } Curly brackets Cell array assignment and contents

Use curly braces to construct a cell array, or to access the contents of a particular cell in a cell
array.


  • “Cell Arrays”


To construct a cell array, enclose all elements of the array in curly braces:

C = {[2.6 4.7 3.9], rand(8)*6, 'C. Coolidge'}

Index to a specific cell array element by enclosing all indices in curly braces:

A = C{4,7,2}

% Percent


  • Comment

  • Conversion specifier


The percent sign is most commonly used to indicate nonexecutable text within the body of a
program. This text is normally used to include comments in your code.

Some functions also interpret the percent sign as a conversion specifier.

Two percent signs, %%, serve as a cell delimiter as described in “Code Sections” on page 18-7.


  • “Add Comments to Programs” on page 18-4


Add a comment to a block of code:

% The purpose of this loop is to compute
% the value of ...

Use conversion specifier with sprintf:

sprintf('%s = %d', name, value)

2 Program Components

Free download pdf