
- •Matlab r2013a стр. 225
- •Continue Long Statements on Multiple Lines
- •Creating and Concatenating Matrices
- •Overview
- •Constructing a Simple Matrix
- •Entering Signed Numbers
- •Specialized Matrix Functions
- •Examples
- •Concatenating Matrices
- •Keeping Matrices Rectangular
- •Matrix Concatenation Functions
- •Examples
- •Generating a Numeric Sequence
- •The Colon Operator
- •Using the Colon Operator with a Step Value
- •Matrix Indexing
- •Accessing Single Elements
- •Linear Indexing
- •Functions That Control Indexing Style
- •Accessing Multiple Elements
- •Nonconsecutive Elements
- •The end Keyword
- •Specifying All Elements of a Row or Column
- •Using Logicals in Array Indexing
- •Logical Indexing – Example 1
- •Logical Indexing – Example 2
- •Logical Indexing with a Smaller Array
- •Single-Colon Indexing with Different Array Types
- •Indexing on Assignment
- •Arithmetic Operators
- •Arithmetic Operators and Arrays
- •Operator Precedence
- •Precedence of and and or Operators
- •Overriding Default Precedence
- •Relational Operators and Arrays
- •Relational Operators and Empty Arrays
- •Overview of the Logical Class
- •Logical Operators
- •Element-Wise Operators and Functions
- •Short-Circuit Operators
- •Precedence of and and or Operators
- •Symbol Reference
- •Asterisk — *
- •Filename Wildcard
- •Function Handle Constructor
- •Class Folder Designator
- •Line Continuation
- •Dynamic Structure Fields
- •Exclamation Point — !
- •Semicolon — ;
- •Array Row Separator
- •Output Suppression
- •Command or Statement Separator
- •Single Quotes — ' '
- •Square Brackets — [ ]
- •Fundamental matlab Classes
- •More About
- •Overview of Numeric Classes
- •Integers
- •Integer Classes
- •Creating Integer Data
- •Arithmetic Operations on Integer Classes
- •Largest and Smallest Values for Integer Classes
- •Integer Functions
- •Floating-Point Numbers
- •Double-Precision Floating Point
- •Single-Precision Floating Point
- •Creating Floating-Point Data
- •Creating Double-Precision Data
- •Creating Single-Precision Data
- •Arithmetic Operations on Floating-Point Numbers
- •Double-Precision Operations
- •Single-Precision Operations
- •Largest and Smallest Values for Floating-Point Classes
- •Largest and Smallest Double-Precision Values
- •Largest and Smallest Single-Precision Values
- •Accuracy of Floating-Point Data
- •Double-Precision Accuracy
- •Single-Precision Accuracy
- •Avoiding Common Problems with Floating-Point Arithmetic
- •Example 1 — Round-Off or What You Get Is Not What You Expect
- •Example 2 — Catastrophic Cancellation
- •Example 3 — Floating-Point Operations and Linear Algebra
- •Floating-Point Functions
- •Creating a Rectangular Character Array
- •Combining Strings Vertically
- •Combining Strings Horizontally
- •Identifying Characters in a String
- •Working with Space Characters
- •Expanding Character Arrays
- •String Comparisons
- •Comparing Strings for Equality
- •Comparing for Equality Using Operators
- •Categorizing Characters Within a String
- •Create a Structure Array
- •Access Data in a Structure Array
- •Concatenate Structures
- •Generate Field Names from Variables
- •Access Data in Nested Structures
- •Access Elements of a Nonscalar Struct Array
- •Create a Cell Array
- •Access Data in a Cell Array
- •Add Cells to a Cell Array
- •Delete Data from a Cell Array
- •Combine Cell Arrays
- •Pass Contents of Cell Arrays to Functions
- •Multilevel Indexing to Access Parts of Cells
- •Related Examples
- •What Is a Function Handle?
- •Creating a Function Handle
- •Maximum Length of a Function Name
- •The Role of Scope, Precedence, and Overloading When Creating a Function Handle
- •Obtaining Permissions from Class Methods
- •Example
- •Using Function Handles for Anonymous Functions
- •Arrays of Function Handles
- •Calling a Function Using Its Handle
- •Calling Syntax
- •Calling a Function with Multiple Outputs
- •Returning a Handle for Use Outside of a Function File
- •Example — Using Function Handles in Optimization
- •Preserving Data from the Workspace
- •Preserving Data with Anonymous Functions
- •Preserving Data with Nested Functions
- •Loading a Saved Handle to a Nested Function
- •Applications of Function Handles
- •Example of Passing a Function Handle
- •Pass a Function to Another Function
- •Example 1 — Run integral on Varying Functions
- •Example 2 — Run integral on Anonymous Functions
- •Example 3 — Compare integral Results on Different Functions
- •Capture Data Values For Later Use By a Function
- •Example 1 — Constructing a Function Handle that Preserves Its Variables
- •Example 2 — Varying Data Values Stored in a Function Handle
- •Example 3 — You Cannot Vary Data in a Handle to an Anonymous Function
- •Call Functions Outside of Their Normal Scope
- •Save the Handle in a mat-File for Use in a Later matlab Session
- •Parameterizing Functions
- •Overview
- •Parameterizing Using Nested Functions
- •Parameterizing Using Anonymous Functions
- •See Also
- •More About
- •Saving and Loading Function Handles
- •Invalid or Obsolete Function Handles
- •Advanced Operations on Function Handles
- •Examining a Function Handle
- •Converting to and from a String
- •Converting a String to a Function Handle
- •Converting a Function Handle to a String
- •Comparing Function Handles
- •Comparing Handles Constructed from a Named Function
- •Comparing Handles to Anonymous Functions
- •Comparing Handles to Nested Functions
- •Comparing Handles Saved to a mat-File
- •Overview of the Map Data Structure
- •Description of the Map Class
- •Properties of the Map Class
- •Methods of the Map Class
- •Creating a Map Object
- •Constructing an Empty Map Object
- •Constructing An Initialized Map Object
- •Combining Map Objects
- •Examining the Contents of the Map
- •Reading and Writing Using a Key Index
- •Reading From the Map
- •Adding Key/Value Pairs
- •Building a Map with Concatenation
- •Modifying Keys and Values in Map
- •Removing Keys and Values from the Map
- •Modifying Values
- •Modifying Keys
- •Modifying a Copy of the Map
- •Mapping to Different Value Types
- •Mapping to a Structure Array
- •Mapping to a Cell Array
Semicolon — ;
The semicolon can be used to construct arrays, suppress output from a MATLAB command (не для всех), or to separate commands entered on the same line.
Array Row Separator
When used within square brackets to create a new array or concatenate existing arrays, the semicolon creates a new row in the array:
A = [5, 8; 3, 4]
A =
5 8
3 4
Output Suppression
When placed at the end of a command, the semicolon tells MATLAB not to display any output from that command (не для всех). In this example, MATLAB does not display the resulting 100-by-100 matrix:
A = ones(100, 100);
Command or Statement Separator
Like the comma operator, you can enter more than one MATLAB command on a line by separating each command with a semicolon. MATLAB suppresses output for those commands terminated with a semicolon (не для всех команд), and displays the output for commands terminated with a comma.
In this example, assignments to variables A and C are terminated with a semicolon, and thus do not display. Because the assignment to B is comma-terminated, the output of this one command is displayed:
A = 12.5; B = 42.7, C = 1.25;
B =
42.7000
Single Quotes — ' '
Single quotes are the constructor symbol for MATLAB character arrays.
Character and String Constructor
MATLAB constructs a character array from all characters enclosed in single quotes. If only one character is in quotes, then MATLAB constructs a 1-by-1 array.
S = 'Hello World' % Здесь массив 1х11 символов.
For more information, see Characters and Strings (R2013a>MATLAB>Language Fundamentals>Data Types).
Space Character
The space character serves a purpose similar to the comma in that it can be used to separate row elements in an array constructor, or the values returned by a function.
Row Element Separator
You have the option of using either commas or spaces to delimit the row elements of an array when constructing the array. To create a 1-by-3 array, use
A = [5.92 8.13 3.53]
A =
5.9200 8.1300 3.5300
When indexing into an array, you must always use commas to reference each dimension of the array.
Function Output Separator
Spaces are allowed when specifying a list of values to be returned by a function. You can use spaces to separate return values in both function declarations and function calls:
function [data text] = xlsread(file, sheet, range, mode) (выходные аргументы можно также разделять запятой)
Slash and Backslash — / \
The slash (/) and backslash (\) characters separate the elements of a path or folder string. On Microsoft® Windows®-based systems, both slash and backslash have the same effect. On The Open Group UNIX®-based systems, you must use slash only.
On a Windows system, you can use either backslash or slash:
dir([matlabroot '\toolbox\matlab\elmat\shiftdim.m'])
dir([matlabroot '/toolbox/matlab/elmat/shiftdim.m'])
On a UNIX system, use only the forward slash:
dir([matlabroot '/toolbox/matlab/elmat/shiftdim.m'])
Square Brackets — [ ]
Square brackets are used in array construction and concatenation, and also in declaring and capturing values returned by a function.
Array Constructor
To construct a matrix or array , enclose all elements (одинаковых типов, в противном случае произойдет конверсия типов к одному из них) of the array in square brackets:
A = [5.7, 9.8, 7.3; 9.2, 4.5, 6.4] % Здесь однотипные элементы.
Concatenation
To combine two or more arrays into a new array through concatenation, enclose all array elements (одного типа) in square brackets:
A = [B, eye(6), diag([0:2:10])]
Function Declarations and Calls
When declaring or calling a function that returns more than one output, enclose each return value that you need in square brackets:
[data, text] = xlsread(file, sheet, range, mode)
Tilde — ~
The tilde character is used in comparing arrays for unequal values, finding the logical NOT of an array, and as a placeholder for an input or output argument you want to omit from a function call.
Not Equal to
To test for inequality values of elements in arrays a and b for inequality, use a~=b:
a = primes(29)% Функция primes дает prime numbers less than or equal to input value 29: a = 2 3 5 7 11 13 17 19 23 29.
b = [2 4 6 7 11 13 20 22 23 29];
not_prime = b(a~=b) % Поэлементное сравнение:
not_prime =
4 6 20 22
Logical NOT
To find those elements of an array that are zero, use:
a = [35 42 0 18 0 0 0 16 34 0];
~a % Логический результат запроса:
ans =
0 0 1 0 1 1 1 0 0 1
Argument Placeholder
To have the fileparts (Parts of file name and path) function return its third output value and skip the first two, replace arguments one and two with a tilde character:
[~, ~, filenameExt] = fileparts(fileSpec); % Ниже - блок комментарий:
%{
в общей форме [pathstr, name, ext]= fileparts(filename)returns the path name pathstr, file name name, and extension ext for the specified file filename. The file does not have to exist. filename is a string enclosed in single quotes. The returned ext field contains a dot (.) before the file extension.
%}
ПРИМЕР.
file = 'H:\user4\matlab\myfile.txt';
[pathstr, name, ext] = fileparts(file)
pathstr =
H:\user4\matlab
name =
myfile
ext =
.txt (Конец ПРИМЕРА)
See Ignore Function Outputs (R2013a>MATLAB>Language Fundamentals>Special Characters) in the MATLAB Programming documentation for more information.