How to implement text manipulation with Matlab
Editor to share with you how to achieve Matlab text operation, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
1. Fopen and fprintf function fopen ()
Fopen () is a function that reads data into matlab in the specified format. The common calling format is:
Fid=fopen ('filename','permission') or fid=fopen (' filename')
Where fid is the file handle, filename is the file name to be opened, and permission is opened. The default opening method is "read-only". After using fopen (), a file code is returned to the fid variable by default. The return value is usually as follows:
Fid=+N (N is a positive integer): indicates that the file was opened successfully, and the file code is N.
Fid=-1, indicating that the file failed to open. File ID is always valid until this file is closed.
Permission is the parameter for the opening method:
R: read out
W: write (automatically created if the file does not exist)
A: subsequent writes (automatically created if the file does not exist)
Ringing: read and write (the file should already exist)
Write refresh: refresh the write (automatically create the file if it does not exist)
Awrite: subsequent writes (automatically created if the file does not exist).
W: rewrite, but not automatically refresh
A: subsequent writes, but not automatic refresh
Rt: open text to read in text format
Wt: open a file to write in text format
Fprintf
The fprintf () function can write data to a specified text file in a specified format, in the following basic format:
Fprintf (fid,format,A)
Fid is a file handle, obtained from the fopen () function, and format is used to specify the format in which the data is output:
% d: integer
% e: real number: scientific calculation form
% f: real number, decimal form
% g: one of the above two formats is automatically selected by the system.
% s: output string
A: the matrix used to store data.
A certain amount of fighting
Fid=fopen ('d:\ char.txt','w')
Fprintf (fid,'%s',a)
Fclose (fid)
Method 1. Write matlab data to txt document: fid = fopen ('data.txt','wt');% data.txt is written file name matrix = yCopy;% M is the matrix to be stored [m _ matrix _ n] = size (matrix); for i=1:1:mfor j=1:1:nif _ j _ memory _ fprintf (fid,'%f\ n memory matrix (iMagnej)) Else fprintf (fid,'%f\ nfid); endendendfclose (fid); method 2: fid = fopen ('data.txt',' wt'); mat = Mscape for I = 1:size (mat, 1) fprintf (fid,'%f, mat); fprintf (fid,'\ n'); endfclose (fid) The above is all the content of the article "how to manipulate the text in Matlab". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!