1. Read a line of text with multiple characters in MATLAB
FID = fopen ('');
While ~ Feof (FID)
Tline = fgetl (FID );
[Row Col] = size (tline );
Print = findstr (tline, 'print ');
Vein = findstr (tline, 'vein ');
User_id = findstr (tline, 'user _ id ');
P_value = str2num (tline (1, print + 6: vein-2 ));
V_value = str2num (tline (1, vein + 5: user_id-2 ));
Plot (p_value, v_value, 'r *');
End;
2. Extract specific characters After MATLAB reads the document
FID = fopen ('d: \ 360downloads \ 11 \ result.txt ');
Tline = fgetl (FID );
[Row Col] = size (tline );
Print = findstr (tline, 'print ');
Vein = findstr (tline, 'vein ');
User_id = findstr (tline, 'user _ ID: 44 ');
P_value = str2num (tline (1, print + 6: vein-2 ));
V_value = str2num (tline (1, vein + 5: user_id-2 ));
3. Create a folder and copy file using Matlab
CLC;
Clear;
For k = 1:50
Failname = Dir ('C: \ Documents and Settings \ Administrator \ Desktop \ Users \*.*');
[Row Col] = size (failname );
For I = 3: Row
Path = ['C: \ Documents and Settings \ Administrator \ Desktop \ Users \ 'failname (I). Name];
STR = ['ui _ 'num2str (8 * (k-1) + I-2)];
Savepath = ['C: \ Documents and Settings \ Administrator \ Desktop \ usersfft2 \ 'str];
Copyfile (path, savepath );
End;
End;
Figure (1)
4. MATLAB read and write documents
File Operations in MATLAB files are an important input and output method, that is, reading data from data files or writing results into data files. MATLAB provides a series of low-level input and output functions for file operations. 1. Open and Close a file 1) before opening a file, you must use the fopen function to open or create a file and specify the operation method for the file. FID = fopen (file name, 'open method ') Description: FID is used to store the file handle value. If the returned handle value is greater than 0, the file is successfully opened. The file name uses a string to indicate the data file to be opened. Common open methods are as follows :? 'R': open a file in read-only mode (default mode). The file must already exist .?
'R + ': open the file in read/write mode. Read the file before writing it. The file must already exist. ? 'W': write data after opening. If the file already exists, update it. If the file does not exist, create it .? 'W + ': open a file in read/write mode. First read and then write. If the file already exists, update it. If the file does not exist, create it. ? 'A': add data at the end of the opened file. If the file does not exist, it is created .? 'A + ': After opening the file, read the data before adding the data. If the file does not exist, it is created. In addition, add a "T" after these strings, such as 'rt 'or 'wt +', to open the file in text; if "B" is added ", it is opened in binary format, which is also the default open method of the fopen function. 2) close the file after reading, writing, and other operations, should be closed in time to avoid data loss. Use the fclose function to close the file. The call format is sta = fclose (FID)
Note: This function disables the files represented by FID. Sta indicates the code returned when the file operation is disabled. If the operation is successful, 0 is returned; otherwise,-1 is returned. To close all opened files, use fclose ('all '). 2. Binary file read/write operations 1) The fwrite function writes elements in the Matrix to the file based on the specified data precision. The call format is: Count = fwrite (FID, A, precision) Description: Where count returns the number of data elements written (default), FID is the file handle, A is used to store data written into files. Precision indicates data precision. Common Data Precision includes char, uchar, Int, long, float, and double. The default data precision is uchar, that is, the unsigned character format. Example 6.8
Store a binary matrix into a disk file.> A = [1 2 3 4 5 6 7 8 9];> FID = fopen ('d: \ test. bin', 'wb ') % open the file FID = 3% in binary data writing mode. The value is greater than 0, indicating that the file is successfully opened> fwrite (FID, A, 'double ') ans = 9% indicates that 9 data is written> fclose (FID) ans = 0% indicates that the service is disabled successfully. 2) The fread function of the binary file can read data from the binary file, and store the data to the matrix. The call format is [a, Count] = fread (FID, size, precision)
Note: A is the matrix used to store read data, count is the number of data elements returned, FID is the file handle, and size is optional. If not selected, the entire file content is read; if selected, the value can be n (read n elements to a column vector), INF (read the entire file), [m, n] (read data to the m × n matrix, where data is stored by column ). Precision is used to control the precision of written data in the same form as the fwrite function. 3. read/write operations on text files (1) the fscanf function for reading text files can read the content of text files and store them in a matrix in the specified format. The call format is [a, Count] = fscanf (FID, format, size)
Note: A is used to store the read data. Count returns the number of read data elements. FID is the file handle. format is used to control the read data format. It consists of % and a format character, common Format characters include D (integer type), F (floating point type), S (string type), and C (character type, you can also insert additional format specifiers, such as data width descriptions, between % and format operators. Size is optional and determines the data arrangement in matrix A. It can take the following values: n (read n elements to a column vector), INF (read the entire file), [m, n] (read data to the m × n matrix, where data is stored by column ). 2) The fprintf function can write data to a text file in the specified format. The call format is fprintf (FID, format, a) Description: FID is the file handle and specifies the file to write data. format is used to control the format of the written data, similar to the fscanf function, A is a matrix used to store data. Example 6.9
Create a character matrix and store it to the disk. Then read and assign a value to another matrix.> A = 'string'; >>> FID = fopen ('d: \ char1.txt ', 'w'); >>> fprintf (FID,' % s', a); >> fclose (FID); >>> fid1 = fopen ('d: \ char1.txt ', 'rt'); >> fid1 = fopen ('d: \ char1.txt ', 'rt '); >> B = fscanf (fid1,' % s') B = string MATLAB TXT file fidbench fopen('fx.txt ', 'R'); % get the file number [F, count] = fscanf (FID, '% F
% F', []); % read the data of file number 1 to F. Where F is the matrix of [12 90] % here '% F found) I personally think it is better to use the first method. For some time, it is especially troublesome to use load when one line of fx.txt contains multiple data.
5. Matla copy the entire folder and the following content
[Row Col] = size (failname );
For I = 1: Row
Path = failname {I };
Len = length (PATH );
Startpoint = Len;
While (path (startpoint )~ = '\')
Startpoint = startpoint-1;
End;
STR = [];
For I = startpoint + 1: Len
STR = [STR path (I)];
End
Savepath = ['d: \ failimage \ 'str];
Copyfile (path, savepath );
End;
Figure
6. Cell reading in MATLAB
Names = {'fyc', 'hy ', 'ljg', 'lqf', 'lsl', 'ml', 'nhz ', 'rj', 'syj ', 'wl ', 'wq', 'wyc', 'xch', 'xxj', 'yjf ',
'Zc', 'zdx ', 'zjg', 'zl', 'zyf '};
Len_names = length (names );
For I = 1: len_names
Url = strcat ('d: \ gaitdataseta-silh \ silhouettes \ ', names (I),' \ 00_1 ');
Url = URL {1}; % WillCellType conversion to string type
End
As shown above, the URL calls strcat ('d: \ gaitdataseta-silh \ silhouettes \ ', names (I),' \ 00_1 ');CellType Variable, you need to make some changes to the URL at this time, use url = URL {1}; that's done!
7. MATLAB reading characters in the document
CLC
Filename = fopen ('e: \ filename.txt', 'r'{}}open the 4*3 array in the document shiyan.txt in the current directory.
FID = fgetl (filename );
Image1 = imread (FID );
FD = fopen ('e: \ points.txt ', 'R'); // open the document shiyan.txt in the current directory and store 4*3 arrays.
A = fscanf (FD, '% F'); % // read the 12 data and save it in vector
For J = 1:4
For I = 1:2
B (J, I) = a (J-1) * 2 + I); % // transfer to 4*3 matrix B
End;
End;
Figure (1)
Subplot (1, 2 );
Imshow (image1 );
Hold on
For J = 1:4
Plot (B (J, 1), 480-b (J, 2 ),'*');
End;
Hold off;
Filename = fopen ('e: \ filename_ir.txt', 'r'{}}open the 4*3 array in the document shiyan.txt in the current directory.
FID = fgetl (filename );
Image1 = imread (FID );
FD = fopen ('e: \ points_ir.txt ', 'R'); // open the document shiyan.txt in the current directory and store 4*3 arrays.
A = fscanf (FD, '% F'); % // read the 12 data and save it in vector
For J = 1:4
For I = 1:2
B (J, I) = a (J-1) * 2 + I); % // transfer to 4*3 matrix B
End;
End;
Subplot (1, 2 );
Imshow (image1 );
Hold on
For J = 1:4
Plot (B (J, 1), 480-b (J, 2 ),'*');
End;
Hold off;
8. Use unknown characters in MATLAB operations
You can declare this character with Syms before using this character in computing. For example:
> Syms;
> B = [cos (a) sin (a);-sin (a) Cos (a)]
B =
[Cos (A), sin (a)]
[-Sin (A), cos (a)]
> Inv (B)
Ans =
[Cos (a)/(COS (a) ^ 2 + sin (a) ^ 2),-sin (a)/(COS (a) ^ 2 + sin () ^ 2)]
[Sin (a)/(COS (a) ^ 2 + sin (a) ^ 2), cos (a)/(COS (a) ^ 2 + sin () ^ 2)]
9. Restore the collected samples by user name
CLC;
Folder_path = 'G: \ 1 \';
Save_path = 'G: \ your rename \';
Folders = Dir (folder_path );
[Row_folder col_folder] = size (folders );
For I = 3: row_folder
Tmp_folderpath = [folder_path folders (I). Name];
Tmp_folderpath = [tmp_folderpath '\'];
Info_path = [tmp_folderpath 'user. info'];
FID = fopen (info_path, 'rb ');
While ~ Feof (FID)
Tline = fgetl (FID );
T = findstr (tline, 'caption ');
If (t)
[Row1 col1] = size (tline );
Tt = tline (9: col1 );
Save_path1 = [save_path TT]; % new folder name
Save_path1 = [save_path1 '\']; % new folder name
Break;
End;
End;
Fclose (FID );
% Copy file
Source_path = [tmp_folderpath 'sample \ '];
Copyfile (source_path, save_path1 );
% TMP = [source_path '*. BMP'];
% Tmp2 = Dir (TMP );
% [Row1 col1] = size (tmp2 );
% For j = 1: row1
% Source_path = [source_path tmp2 (j). Name];
% Savepath1 = [savepath tmp2 (j). Name];
% Copyfile (source_path, savepath1 );
% End;
End;