Detailed explanation of the usage of fprintf function in matlab:
The fprintf function can write data to a text file in a specified format. The calling format is:
Formatted output of data: fprintf(fid, format, variables)
Output the value of the variable to the screen or specify in the specified format File, fid is the file handle. If it is defaulted, it will be output to the screen.
format is used to specify the format used for data output.
%d integer
%e Real number: scientific calculation method
%f Real number: decimal form
%g The system automatically selects the above One of two formats
%s output string
fprintf(fid,format,A)
Description: fid is the file handle, specifying the file to write data to, format is used The format character used to control the format of the written data is the same as the fscanf function. A is a matrix used to store data.
Example Create a character matrix and save it to disk, then read it out and assign it 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文件 fid=fopen('fx.txt','r'); %得到文件号 [f,count]=fscanf(fid,'%f %f',[12,90]); %把文件号1的数据读到f中。其中f是[12 90]的矩阵 %这里'%f %f'表示读取数据的形势,他是按原始数据型读出 fclose(fid); %关闭文件 另外有的txt文件还可以用load来打开 其语句为 f=load('fx.txt)
Recommended tutorial: "php tutorial"
The above is the detailed content of Detailed explanation of the usage of fprintf function in matlab. For more information, please follow other related articles on the PHP Chinese website!