MATLAB String Operations Summary

Source: Internet
Author: User

MATLAB String Operations Summary

Summary of String Operations

char (S1,s2,...)
Creates an array of characters with a given string or array of cells
Double (S)
Convert a string to an ASC code form
Cellstr (S)
Creates an array of string elements using the given character array
Blanks (N)
Generates a string consisting of n spaces
Deblank (S)
Remove trailing spaces
Eval_r (s) evalc (s)
Use the MATLAB interpreter to find the value of a string expression
Ischar (S)
Judging is not a string array
Iscellstr (C)
Determine if the array is not a string element
Isletter (S)
Judging is not the letter
Isspace (s)
Judging is not a space
strcat (S1,s2,...)
To arrange multiple strings horizontally vertically
Strvcat (S1,s2,...)
To arrange multiple strings vertically
strcmp (S1,S2)
Determine if strings are equal
STRNCMP (S1,s2,n)
Determines whether the first n strings are equal
Strcmpi (S1,S2)
Determine if strings are equal (ignoring case)
Strncmpi (S1,s2,n)
Determines whether the first n strings are equal (ignoring case)
Strtrim (S1)
Delete the trailing space
Findstr (S1,S2)
Find
Strfind (S1,S2)
Find S2 in S1 species
Strjust (S1,type)
Adjusts a string array by the specified type
Strmatch (S1,S2)
To find the subscript for a required string
Strrep (S1,S2,S3)
Replace the S2 that appear in the string S1 with S3
Strtok (S1,D)
Finds a string before and after the first given delimiter in S1
Upper (S)
Capitalize a string
Lower (S)
Converts a string to lowercase
Num2str (k)
Convert a number to a string
Int2str (k)
Convert an integer to a string
Mat2str (k)
Converts a matrix to a string for use by eval
Str2double (S)
To convert a string array to a numeric array
sprintf (S)
Creates a string containing the specified format
SSCANF (S)
Reads a string in the specified control format
C{3} addressing the content
C (3) to the Unit index
(24) Using an expression search
>> str= ' Asdfa sfdgdfg tyefgr tyj sdfg qwfge R rety Yi., m zxfgc fdgh fgh ';
>> regexp (str, ' FG ')
Ans =
12 18 29 34 54 63
We can use a character class to match a specified character, such as a letter, a number, or a space character, or it can be used to match a set of characters. One of the most useful character classes is a period (.), which is used to represent any single character. Another useful character class is a sequence of characters in square brackets or a portion of a character, which is used to denote a subset of strings that match any one of the character elements in the square brackets. For example:
We want to look for the first character in Str as D, the last character is a three-character combination of f or G, you can use the following code,
>> regexpi (str, ' D.[FG] ')
Ans =
11 28
>> regexpi (str, ' s.[fg] ', ' match ') listed to
Ans =
' SDF ' ' SDF '
>> regexp (str, ' [a-d] ')
Ans =
1 3 5 9 11 28 56 59 List a-d All
(25) Relational operations:
XOR (x, y) XOR relationship
One of any (x) x is nonzero, and the result is a
All (x) x is zero if one is zero.
b=b+ (b==0) *eps, which avoids the occurrence of zero
>> B=-2:2
B =
-2-1 0 1 2
>> X=sin (B)./b
Warning:divide by zero.
x =
0.4546 0.8415 NaN 0.8415 0.4546
>> b=b+ (b==0) *eps
B =
-2.0000-1.0000 0.0000 1.0000 2.0000
>> X=sin (B)./b
x =
0.4546 0.8415 1.0000) 0.8415 0.4546
(26) Process Control
Switch expression
Case Test_expression
(COMMANDS1)
Case Test_expression
(COMMANDS2)
Case Test_expression
(COMMANDS3)
..
End
Try
(COMMANDS1)
Catch
(Command2)
End
Here, the commands in the (COMMANDS1) are executed, and if there is no error, the program control jumps directly to end. If yo error, program control wine jumps immediately to catch statement. Executes an expression (COMMANDS2).
You can use the LastErr function to return code execution encountered in a river error:
>> matlab (:)
??? Undefined function or variable "matlab".
>> LastErr
Ans =
Undefined function or variable "matlab".
In addition, LastError can be used to return more detailed error information to a struct:
>> Errstr=lasterror
Errstr =
Message: ' Undefined function or variable ' matlab '. '
Identifier: ' Matlab:undefinedfunction '
Stack: [0x1 struct]
>> Errstr.message
Ans =
Undefined function or variable "matlab".
>> (27) function
Error (' AFGFADGSDF ') terminates the program and returns the contents of the parentheses
Error and fprintf can format the displayed numeric variables in a string
Warning () Just as the program continues to run.
Pcode MyFunction which myfunction is the M file to be compiled, this command can be used to create a pseudo-code file to improve the speed of operation.
(28) Create a user's own toolbox
When a project is good, we can put it in the Toolbox folder, so that Matlab can take it to the cache area, to improve the speed. In order to facilitate file management, and future maintenance, we'd better add two script files: README.M and contents.m These two files do nothing, and all are made up of some comment lines.
Getpref get setpref settings, addpref add rmpref Delete, Ispref, Uigetpref, Uisetpref Toolbox or function priority
(30) Create a command:
Just look at the edit command and you'll know.
(31) function handle and anonymous function:
>> il_humps=inline (' 1./(x-.3) ^2+.01) +1./((x-.9). ^2+.04) -6′, ' x ')
Il_humps =
Inline function:
Il_humps (x) = 1./((x-.3). ^2+.01) +1./((x-.9). ^2+.04)-6
The inline function creates a function from a string and takes x as the input variable.
To validate a string representation of a function or an inline function, you can use the Feval function.
>> z=feval_r (' Sin ', pi* (0:4)/4)
z =
0 0.7071 1.0000) 0.7071 0.000
>> Z=feval_r (il_humps,[-1 0 1])
z =
-5.1378 5.1765 16.0000
An anonymous function is created below:
>> [Email protected] (x) 1./((x-.3) ^2+.01) +1./((x-.9). ^2+.04)-6
Af_humps =
@ (x) 1./((x-.3). ^2+.01) +1./((x-.9). ^2+.04)-6
>> Af_humps ( -1:1)
Ans =
-5.1378 5.1765 16.0000
>> a=1;b=2;
>> [Email protected] (x) sin (a*x). *cos (b*x);
>> Asda ( -1:1)
Ans =
0.3502 0-0.3502
>> fhan={@sin @cos}
Fhan =
@sin @cos
>> Fhan{1} (1:2)
Ans =
0.8415 0.9093
Str2func ()
Func2str ()
(32) Nested functions
If a function contains nested functions, either the nested function or the function must end with an end:
The difference between a nested function and a child function:
The nesting occurs anywhere after the main function declaration, and the child function can only appear after the main function.
Nesting can access any variable value in the main function's workspace, and child functions can access only certain variables of the main function's workspace through parameters.
The main function can access all the variables in the nested function's workspace, but only through the output of the child functions to access certain variables of the child function's workspace
Commissioning and profiling of M files
Keyboard
Mlint
Depfun
Using profile to analyze the efficiency of the program
One way: Directly open profile, another way to take advantage of this code:
>> Profiles on
>> for i=1:100
Test (1:100);
End
Profile Viewer
() MATLAB data file
Save and load
Save Var1 var2 Var3
Save filename var1 var2
Save (' filename ', ' var1 ', ' var2 ',...)
The load function is the opposite of Save
Vnew=load (' filename ', ' var1 ', ' var2 ') the variable exists in the VNEW structure.
fprintf
>> a=[1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
>>fid = fopen (' Exp.txt ', ' W ');
fprintf (FID, '%2.0f\n ', a);
Fclose (FID);

>> type Exp.txt

1
4
7
2
5
8
3
6
9
The fprintf output is a column-by-column, left-to-right output.
>> dlmwrite (' Eg2.dat ', A, ' \ n ');
>> type Eg2.dat
1
2
3
4
5
6
7
8
9
The fprintf output is the top-to-bottom output of a row.
Reading and writing of files
Clc
Clear All
FID1 = fopen (' F:\example\1.txt ', ' R ');
A=fgetl (FID1);
Fclose (FID1);
Fid2 = fopen (' F:\example\2.txt ', ' a ');
fprintf (Fid2, '%s\n ', a);
Fclose (FID2);
Type F:\example\2.txt% Displays the contents of the text
fprintf (FID, '%s\r\n ', x); % of the case can be the data output of the file and wrap

MATLAB String Operations Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.