MATLAB operations (Summary 1)

Source: Internet
Author: User

 

1. MATLAB File Processing 1

1. Open and Close file 1

2. Read File 1

3. Move file pointer 2

4. Example 2 of File Operations

Ii. MATLAB character file processing 2

1. Read and Write TXT files 2

Iii. MATLAB string processing 2

1. Get String Length 2

2. connection string 2

3. Search for string replacement 3

4. String Conversion 3

5. String sorting 3

Iv. Cell and struct 3

1. cell array 3

2. structure array (struct array) 3

V. MATLAB directory operation 4

1. MATLAB File Processing 1Open and Close files

FID = fopen (File Name, open mode)

Open methods include:R (Read),W (Write),A (Append to the end of the file)

The file handle is returned.

Sta = fclose (FID)

Close a file by using a file handle. Success0Otherwise, return-1

Feof (FID)

Determine whether the file is reached. Yes, returnTrue.

2 Reading files

(Note:FIDIndicates the file handle.)

(1) read and write in binary format: (read and write binary files)

[A, Count] = fread (FID, size, pre)-View help details. The read data is stored inAMedium,CountThe number of data reads stored.SizeOptional. The entire file is read by default when no data is written.

Count = fwrite (FID, A, pre)-View help details.ATo store the data to be written,CountReturns the number of successful writes.

 

(2) formatted and read/write: (read and write text files)

[A, Count] = fscanf (FID, format, size) -view help details. A medium, count Number of data read by storage (count can be omitted, A = fscanf (FID, format, size) , size optional. The entire file is read by default when no data is written. The format is % s, % C, % d, % F .

Count = fprintf (FID, format,)-View help details.ATo store the data to be written,CountReturns the number of successful writes.

(3) Other read/write Methods

Read a row:Fgetl (FID)

 

3 , Move the file pointer

Status = fseek (FID, offset, origin)-View help details.OffsetIndicating the Moving position, OriginIndicates the original location.

4 File Operation example

FID = fopen (infile, 'R'); % open the file

While ~ Feof (FID)

STR = fgetl (FID); % read a row

% Determines whether a row starts with a number. If yes, the operation is performed.

If length (STR)> = 1 & Double (STR (1)> = 48 & Double (STR (1) <= 57

Fprintf (foutfile, '% s \ n', STR); % write a row of data to the file

End

End

Tempdata = importdata (OUTFILE );

% WriteTxtThe data in the file is read, and the format is good.

 

Ii. MATLAB Character File Processing 1 , Txt File read/write

Importdata (filename)

III, MATLAB String processing 1 , Returns the string length.

Length (STR)

2 , Connection string

Concatenate a string: the rightmost space of each string is trimmed and the result is a string.

Strcat (A, B).

For example:A ="Qwe"; B = "A"; C = "etret"; D = strcat (A, B)The result is:Qwea

Concatenate multiple strings: The result is in the form of a two-dimensional string array. The short string is automatically left blank so that the length is the same as the longest character.

Strvcat (A, B, C).

For example:A ="Qwe"; B = "A"; C = "etret"; D = strvcat (A, B, C)The result is:

Qwe

A

Etret

3String replacement search

Strrep (STR, str2, str3). SetStrInStr1ReplaceStr2.

Strfind (STR, str1). InStrSearchStr1. Returns the location array.

4 , String Conversion

Convert numbers to strings:Num2str ()

Convert a string to a number:Str2num ()

Convert the letters in the string to lowercase:Lower ()

Convert the letters in the string to lowercase:Upper ()

5 , String sorting

According to the stringASCIICode sorting:Sort ()

Iv. tuples Cell And Structure Struct 1, Cell array ( CELL ARRAY )

Each element is (Cell).CellCan contain any typeMATLABData.

A cell array can contain differentCell.

(1) CreateCellArray

ExploitationCellFunctions, such:Cell (2, 3)Generate2*3OfCellArray. Or directly equal to a single tuples, such:C = {[1 2 3 4]}.

(2) Access unit array: Two Methods

Use brackets and curly brackets.

The Unit (Cell). The specific content of the unit is obtained using curly brackets.

For example:B = cell (1); B {1} = [1 2 3 4]. When displayedB (1)Obtain[1x4 double]. And useB {1}Get1 2 3 4.

(3) Assignment: with the method previously accessed, you can understand that there are two ways to assign a value.

When using parentheses, you mustCellTo the left. You can directly assign the value to the left when using curly brackets.

 

2 , Structure array ( Struct Array )

(1) Create structure and structure tuples

UseStructCreate,Struct ('fieldname1', value1, 'field2', value2 ,....);In addition, you can create a structure in the right-hand mode:G. A = 1; G. B = 2

CreateStructArray: directly generate an array using the structure:[A B C...]WhereA, B, cAllThe same structure.

(2) access structure tuples

Use parentheses to access the structure. Point operations are used for structure access.

(3) Assignment

Based on the previous basic operations, you can combine the operations with brackets.

 

Typical applications include directory operations.

Files = Dir ('path'); the list of files in the directory, that is,Struct.N * 1OfStructArray.

Access:Files (1). NameThe name of the first file.

 

 

V. MATLAB Directory operations

Dir (dirname)Read the list of objects in a directory. The returned value isStructColumn vector.

therefore, struct knowledge. Then, we can work well with the knowledge of string processing.

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.